[R] Vectorised operations
William Dunlap
wdunlap at tibco.com
Wed May 18 16:26:01 CEST 2016
ave(A, i, FUN=cummax) loops but is faster than your aggregate-based
solution. E.g.,
> i <- rep(1:10000, sample(0:210, replace=TRUE, size=10000))
> length(i)
[1] 1056119
> a <- sample(-50:50, replace=TRUE, size=length(i))
> system.time( vAve <- ave(a, i, FUN=cummax) )
user system elapsed
0.13 0.03 0.16
> system.time( vAggregate <-
as.vector(unlist(aggregate(a,list(i),cummax)[[2]])) )
user system elapsed
1.81 0.13 1.98
> all.equal(vAve, vAggregate)
[1] TRUE
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Wed, May 18, 2016 at 6:32 AM, John Logsdon <
j.logsdon at quantex-research.com> wrote:
> Folks
>
> I have some very long vectors - typically 1 million long - which are
> indexed by another vector, same length, with values from 1 to a few
> thousand, sp each sub part of the vector may be a few hundred values long.
>
> I want to calculate the cumulative maximum of each sub part the main
> vector by the index in an efficient manner. This can obviously be done in
> a loop but the whole calculation is embedded within many other
> calculations which would make everything very slow indeed. All the other
> sums are vectorised already.
>
> For example,
>
> A=c(1,2,1, -3,5,6,7,4, 6,3,7,6,9, ...)
> i=c(1,1,1, 2,2,2,2,2, 3,3,3,3,3, ...)
>
> where A has three levels that are not the same but the levels themselves
> are all monotonic non-decreasing.
>
> the answer to be a vector of the same length:
>
> R=c(1,2,2, -3,5,6,7,7, 6,6,7,7,9, ...)
>
> If I could reset the cumulative maximum to -1e6 (eg) at each change of
> index, a simple cummax would do but I can't see how to do this.
>
> The best way I have found so far is to use the aggregate command:
>
> as.vector(unlist(aggregate(a,list(i),cummax)[[2]]))
>
> but rarely this fails, returning a shorter vector than expected and seems
> rather ugly, converting to and from lists which may well be an
> unnecessary overhead.
>
> I have been trying other approaches using apply() methods but either it
> can't be done using them or I can't get my head round them!
>
> Any ideas?
>
> Best wishes
>
> John
>
> John Logsdon
> Quantex Research Ltd
> +44 161 445 4951/+44 7717758675
>
> ______________________________________________
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list