[R] Speed up or alternative to 'For' loop

arun smartpink111 at yahoo.com
Tue Jun 11 01:27:04 CEST 2013


Hi,
Some speed comparisons:


df <- data.frame(TreeID=rep(1:6000,each=20), Age=rep(seq(1,20,1),6000))
df$Height <- exp(-0.1 + 0.2*df$Age)
df1<- df
df3<-df
library(data.table)
dt1<- data.table(df)
df$HeightGrowth <- NA 


system.time({  #Rui's 2nd function
df2 <- data.matrix(df)
for(i in seq_len(nrow(df2))[-1]){
    if(df2[i, "TreeID"] == df2[i - 1, "TreeID"])
        df2[i, "HeightGrowth"] <- df2[i, "Height"] - df2[i - 1, "Height"]
}
})
# user  system elapsed 
 # 1.108   0.000   1.109 


system.time({for (ir in unique(df$TreeID)) {   #Don's first function
  in.ir <- df$TreeID == ir
  df$HeightGrowth[in.ir] <- c(NA, diff(df$Height[in.ir]))
}})
#  user  system elapsed 
#100.004   0.704 100.903 

system.time({df3$delta <- c(NA,diff(df3$Height)) ##Don's 2nd function
df3$delta[df3$delta < 0] <- NA}) #####winner 
#   user  system elapsed 
 # 0.016   0.000   0.014 

system.time(df1$HeightGrowth <- ave(df1$Height, df1$TreeID, FUN= function(vec) c(NA, diff(vec)))) #David's
 #user  system elapsed 
 # 0.136   0.000   0.137 
 system.time(dt1[,HeightGrowth:=c(NA,diff(Height)),by=TreeID])
#  user  system elapsed 
 # 0.076   0.000   0.079 


 identical(df1,as.data.frame(dt1))
#[1] TRUE
 identical(df1,df)
#[1] TRUE


head(df1,2)
#  TreeID Age   Height HeightGrowth
#1      1   1 1.105171           NA
#2      1   2 1.349859    0.2446879
head(df2,2)
#     TreeID Age   Height HeightGrowth
#[1,]      1   1 1.105171           NA
#[2,]      1   2 1.349859    0.2446879

A.K.



----- Original Message -----
From: Trevor Walker <trevordaviswalker at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, June 10, 2013 1:28 PM
Subject: [R] Speed up or alternative to 'For' loop

I have a For loop that is quite slow and am wondering if there is a faster
option:

df <- data.frame(TreeID=rep(1:500,each=20), Age=rep(seq(1,20,1),500))
df$Height <- exp(-0.1 + 0.2*df$Age)
df$HeightGrowth <- NA   #intialize with NA
for (i in 2:nrow(df))
{if(df$TreeID[i]==df$TreeID[i-1])
  {df$HeightGrowth[i] <- df$Height[i]-df$Height[i-1]
  }
}

Trevor Walker
Email: trevordaviswalker at gmail.com

    [[alternative HTML version deleted]]

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




More information about the R-help mailing list