Using Ted Harding's example: news1o <- runif(1000000) s2o <- runif(1000000)
pt1 <- proc.time() s <- numeric(length(news1o))-1 # Set all of s to -1 s[news1o>s2o] <-1 # Change to 1 only those values of s # for which news1o>s2o pt2<- proc.time() pt2-pt1 # Takes even less time... # user system elapsed # 0.04 0.00 0.05 >::<>::<>::<>::<>::<>::<>::<>::<>::<>::<>::<>::< Please note: I will be out of the office and out of email contact from 7/11-7/25/2010 >::<>::<>::<>::<>::<>::<>::<>::<>::<>::<>::<>::< Manuela Huso Consulting Statistician 201H Richardson Hall Department of Forest Ecosystems and Society Oregon State University Corvallis, OR 97331 ph: 541-737-6232 fx: 541-737-1393 -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Ted Harding Sent: Monday, July 12, 2010 9:36 AM To: r-help@r-project.org Cc: Raghu Subject: Re: [R] in continuation with the earlier R puzzle 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.hard...@manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 12-Jul-10 Time: 17:36:07 ------------------------------ XFMail ------------------------------ ______________________________________________ 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. ______________________________________________ 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.