Re: [R] For Loops please help!!

2015-09-05 Thread Dan D
Yes, the cause is memory use patterns, but the price is steep nonetheless. E.g.: rate<-log(400*1.1^(1:30)) # runs about 27x times as fast as the following (test via 'microbenchmark') rate<-numeric(30) for (i in 1:30){ rate[i]<-log(400*1.1^i) } When manipulating large arrays, the difference

Re: [R] For Loops please help!!

2015-09-05 Thread Jeff Newmiller
This is not true. The steep price has to do with memory use patterns like result <- c( result, new value ). Vectorization is cleaner, easier to read, and somewhat faster, but for loops are not the monster that they have a reputation for being if the memory is allocated before the loop and elemen

Re: [R] For Loops please help!!

2015-09-05 Thread Dan D
Also, any time you write "for" in R, you pay a steep price in performance. In a short, simple loop it may not be noticeable, but in a more challenging problem it can be a huge issue. A more efficient way to write your loop would be: infectrate = 400*1.1^(1:30) # calculation cbind(1:30,log(infectra

Re: [R] For Loops please help!!

2015-09-05 Thread Dan D
The code has an error so it won't run as written. Instead of: infectrate[n]= (400)(1.1)^(n); try: infectrate[n]= 400*1.1^n; What I get after making this change looks right. -- View this message in context: http://r.789695.n4.nabble.com/For-Loops-please-help-tp4711882p4711884.html Sent from

Re: [R] For loops

2012-04-11 Thread Clemontina Alexander
This has nothing to do with your question, but instead of using >class=c(rep(1,3),rep(2,3),rep(3,3)) > It's probably easier to use class = rep(1:3, each =3) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat

Re: [R] For loops

2012-04-11 Thread MacQueen, Don
In addition to what Jeff and David have said... If you really want to create a separate data frame for each subgroup then you need use the assign function, and also data.split[[i]] instead of data.split[1]. for (i in 1:3) { assign( paste('sub',i,sep='') , data.split[[i]] } Jeff showed you how

Re: [R] For loops

2012-04-09 Thread David Winsemius
On Apr 9, 2012, at 5:33 PM, Christopher Desjardins wrote: Hi, I am having trouble with syntax for a for loop. Here is what I am trying to do. class=c(rep(1,3),rep(2,3),rep(3,3)) out1=rnorm(length(class)) out2=rnorm(length(class)) out3=rnorm(length(class)) data=data.frame(class,out1,out2,out

Re: [R] For loops

2012-04-09 Thread Jeff Newmiller
class1 <- data[1==data$class,] gets you a subset of the data into a dedicated object, but if you want to handle arbitrarily large amounts of data or class values then the list output of split is really much better to stay with. Also, "data" is a predefined function, so it is not a good idea to

[R] For loops

2012-04-09 Thread Christopher Desjardins
Hi, I am having trouble with syntax for a for loop. Here is what I am trying to do. class=c(rep(1,3),rep(2,3),rep(3,3)) out1=rnorm(length(class)) out2=rnorm(length(class)) out3=rnorm(length(class)) data=data.frame(class,out1,out2,out3) dat.split=split(data,data$class) for(i in 1:3){ sub[i]=da

[R] For loops in R

2010-01-17 Thread cjmr
Hello. I've just started using R and am trying to figure out if the two codes snippets below have the same output gBest<-floor(runif(popsize,min=1,max=top)) velocity[i,j]<<-.4* velocity[i,j] + 1 * runif(1) * (pbestsVar[i,j] - popVar[i,j]) + 1 * runif(1) * (archiveVar[gBest,j] - popVar[i,j]) and

[R] help on R for loops

2008-01-26 Thread song xin
Hi, all. I need help on improving the efficiency of my R simulations. Below is a function that simulates a change point model. It first generates a sequence of three dimensional ARMA(1,1) observations, then calculates the one step ahead prediction errors, some statistic is calcualted and compare