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

MacQueen, Don macqueen1 at llnl.gov
Tue Jun 11 00:29:59 CEST 2013


Sorry, it looks like I was hasty.
Absent another dumb mistake, the following should do it.

The request was for differences, i.e., the amount of growth from one
period to the next, separately for each tree.

for (ir in unique(df$TreeID)) {
  in.ir <- df$TreeID == ir
  df$HeightGrowth[in.ir] <- c(NA, diff(df$Height[in.ir]))
}



And this gives the same result as Rui Barradas' previous response.

-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 6/10/13 2:51 PM, "MacQueen, Don" <macqueen1 at llnl.gov> wrote:

>How about
>
>for (ir in unique(df$TreeID)) {
>  in.ir <- df$TreeID == ir
>  df$HeightGrowth[in.ir] <- cumsum(df$Height[in.ir])
>}
>
>Seemed fast enough to me.
>
>In R, it is generally good to look for ways to operate on entire vectors
>or arrays, rather than element by element within them. The cumsum()
>function does that in this example.
>
>-Don
>
>
>-- 
>Don MacQueen
>
>Lawrence Livermore National Laboratory
>7000 East Ave., L-627
>Livermore, CA 94550
>925-423-1062
>
>
>
>
>
>On 6/10/13 10:28 AM, "Trevor Walker" <trevordaviswalker at gmail.com> wrote:
>
>>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.
>
>______________________________________________
>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