[datatable-help] data.table: partition like aggregate (FUN = list)

jim holtman jholtman at gmail.com
Sun Apr 27 19:00:58 CEST 2014


try this:

> dt = data.table (x=10:14, y=20:24)
> # create new 'x' column
> dt[, newX := x]
> dt[
+     , list(x = toString(x)
+         , y = toString(y)
+         )
+     , key = newX %% 3
+     ]
   newX      x      y
1:    0     12     22
2:    1 10, 13 20, 23
3:    2 11, 14 21, 24



Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Sun, Apr 27, 2014 at 7:47 AM, Dan Muresan <danmbox at gmail.com> wrote:

> How do I achieve the following partitioning effect:
>
> dt = data.table (x=10:14, y=20:24)
> aggregate (dt, by = list (dt$x %% 3), FUN = list)
>
>   Group.1      x      y
> 1       0     12     22
> 2       1 10, 13 20, 23
> 3       2 11, 14 21, 24
>
> If I try the following it doesn't work (and I think I know why):
>
> dt [, by = x %% 3, j = list(y)]
>    x  y
> 1: 1 20
> 2: 1 23
> 3: 2 21
> 4: 2 24
> 5: 0 22
>
> (while with j = max (y) it of course works, generating a "V1" column)
>
> Also, how do I name the result of the j-expression (by default the
> resulting column is "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/20140427/47d252fe/attachment.html>


More information about the datatable-help mailing list