Hi, is it possible to do this operation faster? I am going over 35k data entries and this takes quite some time. for(cnt in 2:length(sdata$date)) { if(sdata$value[cnt] < sdata$value[cnt - 1]) { sdata$ddtd[cnt] <- sdata$ddtd[cnt - 1] + sdata$value[cnt - 1] - sdata$value[cnt] } else sdata$ddtd[cnt] <- 0 } return(sdata) Thank you, Benjamin
First, since you only update the 'ddtd' conditioned on 'value', you should be able to vectorize removing the loop. I let you figure out how to do that yourself. Second, you apply the "$" operator multiple times in the loop that will definitely add some overhead. It should be faster to extract 'value' and 'ddtd' and work with those instead. /Henrik On 1/8/07, Benjamin Dickgiesser <dickgiesser at gmail.com> wrote:> Hi, > > is it possible to do this operation faster? I am going over 35k data > entries and this takes quite some time. > > for(cnt in 2:length(sdata$date)) > { > > if(sdata$value[cnt] < sdata$value[cnt - 1]) { > sdata$ddtd[cnt] <- sdata$ddtd[cnt - 1] + sdata$value[cnt - 1] - > sdata$value[cnt] > } > else sdata$ddtd[cnt] <- 0 > > } > return(sdata) > > Thank you, > Benjamin > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Hi Benjamin! ##TRY THIS: THIS MAYBE MUCH FASTER, BECAUSE ONLY WORK WITH THE IMPORTANAT ROWS - HOPE NO ERROR IN IT:D puffer1 <- as.matrix(sdata$value) puffer2 <- rbind(as.matrix(puffer1[2:nrow(puffer1),1]),0) speedy <- puffer1 > puffer2 speedy <- (as.matrix(which(puffer1==TRUE)))+1 sdata$ddtd[]=0 for(cnt in 1:nrow(speedy)) { sdata$ddtd[speedy[cnt,1]] <- sdata$ddtd[(speedy[cnt,1]) - 1] + sdata$value[(speedy[cnt,1]) - 1] -sdata$value[speedy[cnt,1]] } return(sdata) #Zoltan 2007/1/8, Benjamin Dickgiesser <dickgiesser@gmail.com>:> > Hi, > > is it possible to do this operation faster? I am going over 35k data > entries and this takes quite some time. > > for(cnt in 2:length(sdata$date)) > { > > if(sdata$value[cnt] < sdata$value[cnt - 1]) { > sdata$ddtd[cnt] <- sdata$ddtd[cnt - 1] + > sdata$value[cnt - 1] - > sdata$value[cnt] > } > else sdata$ddtd[cnt] <- 0 > > } > return(sdata) > > Thank you, > Benjamin > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >[[alternative HTML version deleted]]
Thanks for your answers, your help is highly appreciated! On 1/8/07, Zoltan Kmetty <zkmetty at gmail.com> wrote:> Hi Benjamin! > > > ##TRY THIS: THIS MAYBE MUCH FASTER, BECAUSE ONLY WORK WITH THE IMPORTANAT > ROWS - HOPE NO ERROR IN IT:D > > puffer1 <- as.matrix(sdata$value) > puffer2 <- rbind(as.matrix(puffer1[2:nrow(puffer1),1]),0) > > speedy <- puffer1 > puffer2 > speedy <- (as.matrix(which(puffer1==TRUE)))+1 > sdata$ddtd[]=0 > > for(cnt in 1:nrow(speedy)) > { > > sdata$ddtd[speedy[cnt,1]] <- sdata$ddtd[(speedy[cnt,1]) - 1] + > sdata$value[(speedy[cnt,1]) - 1] -sdata$value[speedy[cnt,1]] > } > return(sdata) > > > > > #Zoltan > > > > 2007/1/8, Benjamin Dickgiesser <dickgiesser at gmail.com>: > > > > Hi, > > > > is it possible to do this operation faster? I am going over 35k data > > entries and this takes quite some time. > > > > for(cnt in 2:length(sdata$date)) > > { > > > > if(sdata$value[cnt] < sdata$value[cnt - 1]) { > > sdata$ddtd[cnt] <- sdata$ddtd[cnt - 1] + > sdata$value[cnt - 1] - > > sdata$value[cnt] > > } > > else sdata$ddtd[cnt] <- 0 > > > > } > > return(sdata) > > > > Thank you, > > Benjamin > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > > >
Reasonably Related Threads
- Logical operations or selecting data from data.frames
- CMake support for Opus proposal (in addition to Autotools)
- CMake support for Opus proposal (in addition to Autotools)
- Is it possible to extend log message?
- Keeping the data of C structure in R variables?..