[R] Help with speed (replacing the loop?)

Steve Lianoglou mailinglist.honeypot at gmail.com
Wed Jan 11 17:14:11 CET 2012


Hi,

On Wed, Jan 11, 2012 at 10:50 AM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:
> Thanks a lot, Steve. I have one question (below):
>
>
>> ====
>> library(data.table)
>>
>> ## your data
>> xx <- data.table(group=c(rep("group1",5),rep("group2",5)),
>>                 a=1:10, b=seq(10,100,by=10), key="group")
>> yy <- data.table(group=c("group1","group2"), a=c(10,20), b=c(2,3),
>>                 key="group")
>>
>> ## temp data.table to get your ducks in a row
>> m <- merge(xx, yy, by="group", suffixes=c(".x", ".y"))
>
>
> Dimitri: The step above (merge) - I was thinking of it but decided
> against it because my xx already fills up tons of memory. When I merge
> xx and yy that doubles the number of variables - I am afraid my memory
> won't hold that much stuff...

Fair enough ... how about just using `match`, then, ie:

R> aa <- x$a * y$a[match(x$group, y$group)]

Should do the trick, no?

-steve

-- 
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 R-help mailing list