[datatable-help] Running on variables in data.table

Matt Dowle mdowle at mdowle.plus.com
Thu Oct 3 10:38:42 CEST 2013


Hi,

Likely :

     dt[,lapply(.SD,sum),by=type]

See the examples section of ?data.table for an example.  `.SD` is 
explained on that page too.

Matt

On 03/10/13 09:25, Kostia wrote:
> Hi,
>
> I have a data table with a number of variables and I wish to do some
> function on each variable,
> my data table looks like this:
>
> type   att1   att2  att3  att4
> black    1        2     2       1
> white    0        2     1       0
> green    4        2     1       0
> black     1        1     1       1
> green    2        1      2       2
>
> I would like to sum on each attribute by type, so my function will be:
>
> dt[,att1type := sum(att1),by = type]
>
> The problem is that I want to taht in a loop and don't know how to run on
> all the columns.
>
> dt[,att1type := sum(dt[[i]]),by = type]
> or
> dt[,att1type := sum(dt[i]),by = type]
>
> doesn't work.
>
> Thanks,
>
> Kostia
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Running-on-variables-in-data-table-tp4677480.html
> Sent from the datatable-help mailing list archive at Nabble.com.
> _______________________________________________
> 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
>



More information about the datatable-help mailing list