[R] R - Vectorization and Functional Programming Constructs
ggrothendieck at gmail.com
Sat Jan 22 13:10:53 CET 2011
On Fri, Jan 21, 2011 at 10:10 PM, Mingo <catojones at gmail.com> wrote:
> Hello, I am new to R (coming from Perl) and have what is, at least at this
> point, a philosophical question and a request for comment on some basic
> code. As I understand it - R emphasizes ,or at least supports, the
> functional programming model. I've come across some code that was markedly
> absent in for loops - and have been seeing some constructs that relate to
> functional programming and vectorized code (not that is at all unique to R
> of course). But I'm also new to the concept of vectorizing code.
> However, since I anticipate dealing with vectors of large sizes I think that
> this approach is probably going to serve well in terms of performance. As an
> example I anticipate having vector operations calling for shifting. I'll be
> shifting vectors to the right (or left) like below while maintaining the
> length and filling with zeros. Keep in mind I'll ultimately be dealing with
> vectors with very large length.
>>x <- c(0,3,2,1,0,0,0)
>>vlen <- length(x)
>  7
> One solution to accomplish the right shift is to do something like:
> 1] 0 0 3 2 1 0 0
> this does the trick though I'm wondering if this is in the spirit of
> "Vectorization". I could make recursive function that would cycle through
> the whole vector eventually leaving it full of 0s thus ending the recursion.
> Though does this capture the spirit of R programming and vectorizing ? Are
> there more primitive operators "closer" to the underlying C code that would
> serve performance interests better ?
If x is supposed to represent a time series that you are trying to
align you would likely be better off to represent it as an object of
one of the time series classes (ts, zoo, xts, timeSeries) and then use
lag. That way you will not only have a convenient lag function but
all the other functionality that you might need to conveniently handle
such objects. lag is written in C in both zoo and xts and might be in
timeSeries as well. If your series is regularly spaced and so
applicable to ts then, internally, lagging only involves manipulating
its "tsp" attribute so it would be extremely fast.
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
email: ggrothendieck at gmail.com
More information about the R-help