[datatable-help] updating subsets

Frank Erickson fperickson at wisc.edu
Mon Jul 20 17:48:11 CEST 2015


I think you should use a single data.table; it's much more straightforward
in that case:

qq <- data.table(runif(18),id=rep(1:2,each=9))
qq[,freq:=.N,by=.(id,seq(nrow(qq))%/%5)]

This differs from your code, but I doubt that you really meant to do by=V1,
since V1 is continuous and therefore is almost guaranteed to have groups of
size one.

--Frank



On Mon, Jul 20, 2015 at 11:39 AM, Sebastian Fischmeister <
sfischme at uwaterloo.ca> wrote:

> Hi,
>
> I'm trying to split a data.table and then update the individual
> splits. Does anyone have an idea, why this doesn't work?
>
> Thanks,
>   Sebastian
>
>
>
> library(data.table)
>
> ## this doesn't work
> q <- list(data.table(runif(9)),data.table(runif(9)))
> lapply( q, function(xx) { qq <- split(xx, 1:nrow(xx) %/% 5)
>                           lapply(qq, function(xx) { xx[, freq:=.N,
> by="V1"] })
>                       })
>
> ## this doesn't work either although freq now exists
> q <- list(data.table(runif(9)),data.table(runif(9)))
> lapply(q, function(xx) xx[,freq:=-1] )
> lapply( q, function(xx) { qq <- split(xx, 1:nrow(xx) %/% 5)
>                           lapply(qq, function(xx) { xx[, freq:=.N,
> by="V1"] })
>                       })
>
>
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20150720/6a92e0c7/attachment.html>


More information about the datatable-help mailing list