[R] Yearly aggregates and matrices

mathijsdevaan mathijsdevaan at gmail.com
Thu Apr 28 22:49:24 CEST 2011


Thanks, but it did not really improve the speed. Why is it that when I change
the layout of the matrix (which does not give the required results), the
speed increases tremendously? So:

library(reshape2)
library(zoo)
z <- read.zoo(DF, split = 3, index = 2, FUN = identity) # Split on 3 and
index on 2 instead of vice versa
sum.na <- function(x) if (any(!is.na(x))) sum(x, na.rm = TRUE) else NA 
r <- rollapply(z, 3,  sum.na, align = "right", partial = TRUE)

or

mm <- melt(DF, id = c("B", "C"))
aa <- acast(mm, B ~ C + variable, FUN = sum) # B ~ C instead of C ~ B
sum.na <- function(x) if (any(!is.na(x))) sum(x, na.rm = TRUE) else NA
r <- rollapply(aa, 3,  sum.na, align = "right", partial = TRUE)

Thanks!





Gabor Grothendieck wrote:
> 
> On Wed, Apr 27, 2011 at 2:03 PM, mathijsdevaan
> <mathijsdevaan at gmail.com> wrote:
>> Hi,
>>
>> Is there an alternative to "z <- read.zoo(DF, split = 2, index = 3, FUN =
>> identity)" and "r <- rollapply(z, 3,  sum.na, align = "right", partial =
>> TRUE)"? I am trying to use the following script in which the split data
>> (B)
>> contains about 300000 unique cases and obviously I am getting an
>> allocation
>> error. Thanks!
>>
> 
> You could test the speed of this to see if its faster:
> 
> library(reshape2)
> library(zoo)
> mm <- melt(DF, id = c("B", "C"))
> aa <- acast(mm, C ~ B + variable, FUN = sum)
> sum.na <- function(x) if (any(!is.na(x))) sum(x, na.rm = TRUE) else NA
> r <- rollapply(aa, 3,  sum.na, align = "right", partial = TRUE)
> 
> 
> -- 
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.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.
> 


--
View this message in context: http://r.789695.n4.nabble.com/Yearly-aggregates-and-matrices-tp3438140p3482174.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list