[R] Fwd: How to make the "apply" faster

Peter Langfelder peter.langfelder at gmail.com
Sat Jul 9 22:32:57 CEST 2016


Forgot to cc the list...

---------- Forwarded message ----------
From: Peter Langfelder <peter.langfelder at gmail.com>
Date: Sat, Jul 9, 2016 at 1:32 PM
Subject: Re: [R] How to make the "apply" faster
To: Debasish Pai Mazumder <pai1981 at gmail.com>


You could try the following (I haven't tested it so check that the
results make sense):

indicator = x>=70;
new = apply(indicator, c(1,2,4), sum);

If you could find a way to make the dimension over which you sum the
last (or first), you could use rowSums (or colSums) on indicator which
would be orders of magnitude faster.

Peter



On Sat, Jul 9, 2016 at 1:19 PM, Debasish Pai Mazumder <pai1981 at gmail.com> wrote:
> I have 4-dimension array x(lat,lon,time,var)
>
> I am using "apply" to calculate over time
>  new = apply(x,c(1,2,4),FUN=function(y) {length(which(y>=70))})
>
> This is very slow. Is there anyway make it faster?
>
> -Debasish
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list