[R] function that calculates using preceding records

Seth Bigelow seth at forestadapt.org
Tue Feb 10 20:44:34 CET 2015


Aha, this solution is even more elegant than that of the previous suggestion. Thanks for alerting me to the capabilities of plyr!

--Seth

-----Original Message-----
From: Dennis Murphy [mailto:djmuser at gmail.com] 
Sent: Tuesday, February 10, 2015 2:14 PM
To: Seth Bigelow
Subject: Re: [R] function that calculates using preceding records

Hi:

Here's another way. If I understand this correctly, you can get the diameters by setting the NA values in d to zero, taking the cumulative sum of d (within treecode) and then subtracting the result from diam[1]. Since I'm used to doing this type of thing in data manipulation packages, I prefer to write a function to do the work and then run it in concert with plyr::ddply().

Letting DF represent the name of your data frame,

# Function to apply to each subgroup (treecode ID) f <- function(d)
  {
      d <- d[order(-d$year), ]         # order by decreasing year
      d$rw[is.na(d$rw)] <- 0          # set NAs in rw to 0
      d$diam <- d$d[1] - cumsum(d$rw)    # compute past diams
      d                                        # return modified data frame
  }

library(plyr)
# This applies the function f to each sub-data frame defined # by unique values of treecode ddply(DF, .(treecode), f)

   treecode year    rw    d   diam
1     TC146 2014 0.000 60.0 60.000
2     TC146 2013 1.750   NA 58.250
3     TC146 2012 1.810   NA 56.440
4     TC146 2011 1.390   NA 55.050
5     TC146 2010 1.940   NA 53.110
6     TC147 2014 0.000 55.5 55.500
7     TC147 2013 1.260   NA 54.240
8     TC147 2012 1.115   NA 53.125
9     TC147 2011 1.025   NA 52.100
10    TC147 2010 1.495   NA 50.605
11    TC148 2014 0.000 34.0 34.000
12    TC148 2013 0.300   NA 33.700
13    TC148 2012 0.335   NA 33.365
14    TC148 2011 0.315   NA 33.050
15    TC148 2010 0.455   NA 32.595
16    TC149 2014 0.000  8.0  8.000
17    TC149 2013 0.080   NA  7.920
18    TC149 2012 0.125   NA  7.795
19    TC149 2011 0.120   NA  7.675
20    TC149 2010 0.125   NA  7.550


HTH,
Dennis

On Tue, Feb 10, 2015 at 6:30 AM, Seth Bigelow <seth at forestadapt.org> wrote:
> Greetings:
>
>
>
> My dataframe has 4 variables: treecode, year, rw (tree ring width), 
> and d (tree diameter). The d variable
>
> only has data for 2014. I wish to calculate earlier diameters by 
> subtracting each year's growth (rw) from the
>
> previous year's diameter, by treecode. Can anyone help me with a 
> function or statement that will do this?
>
> Sample dataset below: In this example, d in year 2013 for treecode 
> TC149 would be 7.92 = 8.0 - 0.080.
>
>
>
> "treecode","year","rw","d"
>
> "1","TC149",2014,NA,8
>
> "2","TC149",2013,0.08,NA
>
> "3","TC149",2012,0.125,NA
>
> "4","TC149",2011,0.12,NA
>
> "5","TC149",2010,0.125,NA
>
> "6","TC148",2014,NA,34
>
> "7","TC148",2013,0.3,NA
>
> "8","TC148",2012,0.335,NA
>
> "9","TC148",2011,0.315,NA
>
> "10","TC148",2010,0.455,NA
>
> "11","TC147",2014,NA,55.5
>
> "12","TC147",2013,1.26,NA
>
> "13","TC147",2012,1.115,NA
>
> "14","TC147",2011,1.025,NA
>
> "15","TC147",2010,1.495,NA
>
> "16","TC146",2014,NA,60
>
> "17","TC146",2013,1.75,NA
>
> "18","TC146",2012,1.81,NA
>
> "19","TC146",2011,1.39,NA
>
> "20","TC146",2010,1.94,NA
>
>
>
>
>
> Seth W. Bigelow
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> 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