[R] How to make this for() loop memory efficient?

Martin Morgan mtmorgan at fhcrc.org
Wed Jan 11 14:52:34 CET 2012


On 01/11/2012 12:09 AM, iliketurtles wrote:
> Ray, your solution works and is indeed faster than mine!
>
> It looks like it's going to take a few days to to 400,000 rows, still, which
> is unfortunate.
>
> Steve, thanks for your help, I'll definitely self-teach plyr and data.table.

I added a column with the first two digits of the module

   data$XX <- substr(L[,2], 1, 2)

then created a data frame that summarized the first module of each call 
and the length of the phone call

   df <- with(data,
              data.frame(FirstModule=tapply(XX, `phone calls`, `[[`, 1),
                         Length=tapply(XX, `phone calls`, length)))

then summarized the length of the phone calls associated with each module

   with(df, tapply(Length, FirstModule, mean))

resulting in

 > with(df, tapply(Length, FirstModule, mean))
       82       84       92       93       94       96       97
1.000000 2.000000 1.750000 1.666667 1.000000 1.222222 1.666667

Martin

>
> -----
> ----
>
> Isaac
> Research Assistant
> Quantitative Finance Faculty, UTS
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-make-this-for-loop-memory-efficient-tp4283594p4284716.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793



More information about the R-help mailing list