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)
[1] 7

One solution to accomplish the right shift is to do something like:

>x=c(0,x[1:vlen-1])
>x
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 ?

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to