[R] in continuation with the earlier R puzzle
(Ted Harding)
Ted.Harding at manchester.ac.uk
Mon Jul 12 18:36:10 CEST 2010
On 12-Jul-10 14:09:30, Raghu wrote:
> When I just run a for loop it works. But if I am going to
> run a for loop every time for large vectors I might as well
> use C or any other language.
> The reason R is powerful is becasue it can handle large vectors
> without each element being manipulated? Please let me know where
> I am wrong.
>
> for(i in 1:length(news1o)){
> + if(news1o[i]>s2o[i])
> + s[i]<-1
> + else
> + s[i]<--1
> + }
>
> --
> 'Raghu'
Many operations over the whole length of vectors can be done
in "vectorised" form, in which an entire vector is changed
in one operation based on the values of the separate elemnts
of other vectors, also all take into account in a single
operation. What happens "behind to scenes" is that the single
element by element operations are performed by a function
in a precompiled (usually from C) library. Hence R already
does what you are suggesting as a "might as well" alternative!
Below is an example, using long vectors. The first case is a
copy of your R loop above (with some additional initialisation
of the vectors). The second achieves the same result in the
"vectorised" form.
news1o <- runif(1000000)
s2o <- runif(1000000)
s <- numeric(length(news1o))
proc.time()
# user system elapsed
# 1.728 0.680 450.257
for(i in 1:length(news1o)){ ### Using a loop
if(news1o[i]>s2o[i])
s[i]<- 1
else
s[i]<- (-1)
}
proc.time()
# user system elapsed
# 11.184 0.756 460.340
s2 <- 2*(news1o > s2o) - 1 ### Vectorised
proc.time()
# user system elapsed
# 11.348 0.852 460.663
sum(s2 != s)
# [1] 0 ### Results identical
Result: The loop took (11.184 - 1.728) = 9.456 seconds,
Vectorised, it took (11.348 - 11.184) = 0.164 seconds.
Loop/Vector = (11.184 - 1.728)/(11.348 - 11.184) = 57.65854
i.e. nearly 60 times as long.
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 12-Jul-10 Time: 17:36:07
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list