[datatable-help] basic data.table query

Santosh Srinivas santosh.srinivas at gmail.com
Sat Dec 4 05:05:32 CET 2010


Thanks. Just getting used to data.table .. was wondering how to get
the middle part (need to use list()) in the brackets ... thanks

On Sat, Dec 4, 2010 at 9:21 AM, Steve Lianoglou
<mailinglist.honeypot at gmail.com> wrote:
> Hi,
>
> On Fri, Dec 3, 2010 at 7:45 PM, Santosh Srinivas
> <santosh.srinivas at gmail.com> wrote:
>> Someone in r-help recommended me to post the question in this group … TIA
>>
>>
>>
>> Hello Group,
>>
>> I need a modification in the data.table example to get my intended
>> result shown below ... is there a more simple way!
>>
>> dt <- data.table(A = rep(1:3, each=4), B = rep(1:4, each=3), C = rep(1:2,
>> 6))
>> dt[, transform(.SD,D=mean(A)), by="B"]
>>
>>
>> The result I want is below ... which is probably long winded!
>>
>> data.table(unique(as.data.frame(dt[, transform(.SD,D=mean(A)),
>> by="B"][,list(B,D)])))
>>     B        D
>> [1,] 1 1.000000
>> [2,] 2 1.666667
>> [3,] 3 2.333333
>> [4,] 4 3.000000
>
> I'm not sure if I'm missing something, but this looks like what you want, no?
>
> R> dt[, list(D=mean(A)), by='B']
>     B        D
> [1,] 1 1.000000
> [2,] 2 1.666667
> [3,] 3 2.333333
> [4,] 4 3.000000
>
> --
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
>  | Memorial Sloan-Kettering Cancer Center
>  | Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact
>


More information about the datatable-help mailing list