Re: [R] Yearly aggregates and matrices

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu, 28 Apr 2011 22:16:52 -0400

On Thu, Apr 28, 2011 at 10:13 PM, Gabor Grothendieck <ggrothendieck_at_gmail.com> wrote:
> On Thu, Apr 28, 2011 at 4:49 PM, mathijsdevaan <mathijsdevaan@gmail.com> wrote:
>> 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)
>>
>
>
> For me it makes little difference:
>
>> system.time(for(i in 1:100) read.zoo(DF, split = 3, index = 2, FUN = identity))
>   user  system elapsed
>   1.72    0.00    1.71
>> system.time(for(i in 1:100) read.zoo(DF, split = 2, index = 3, FUN = identity))
>   user  system elapsed
>   1.75    0.00    1.74

and here it is with rollapply included:

> system.time(for(i in 1:100) { read.zoo(DF, split = 3, index = 2, FUN = identity)
+ r <- rollapply(z, 3, sum.na, align = "right", partial = TRUE)})

   user system elapsed
  14.74 0.00 14.76
.
> system.time(for(i in 1:100) {read.zoo(DF, split = 2, index = 3, FUN = identity)
+ r <- rollapply(z, 3, sum.na, align = "right", partial = TRUE)})

   user system elapsed
  14.72 0.00 14.74

-- 
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.
Received on Fri 29 Apr 2011 - 02:19:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Apr 2011 - 03:00:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive