2

This is an extension of Subtract previous year's from value from each grouped row in data frame. The option using plyr makes complete sense.

Now, I'm trying to add a couple more columns. I've also modified year so it's an actual year with different starting points by id. Here is a link to tab-delimited df: https://dl.dropbox.com/u/9699533/df.txt

enter image description here

I am unable to get a pointer to the previous row when I'm in the current row. I'd like to pass this to the function part of plyr::transform. How do I write this, please? Thanks!

~~~~ Alternate dataframe input ~~~~~

> dput(df)
structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 5L, 5L, 5L), value = c(6L, 16L, 21L, 5L, 10L, 26L, 2L, 
12L, 26L, 9L, 16L, 26L, 2L, 15L, 29L), year = c(2007L, 2008L, 
2009L, 2011L, 2012L, 2013L, 2004L, 2004L, 2006L, 2010L, 2011L, 
2012L, 2014L, 2015L, 2016L), actual = c(6L, 10L, 5L, 5L, 5L, 
16L, 2L, 10L, 14L, 9L, 7L, 10L, 2L, 13L, 14L)), .Names = c("id", 
"value", "year", "actual"), class = "data.frame", row.names = c(NA, 
-15L))
Community
  • 1
  • 1
user1100825
  • 107
  • 1
  • 5

1 Answers1

3

Don't use transform.

foo <- function(x){
   x$ratio <- c(NA,tail(x$actual,-1)/head(x$value,-1))
   x
 }
> 
> ddply(df,.(id),foo)
   id value year actual     ratio
1   1     6 2007      6        NA
2   1    16 2008     10 1.6666667
3   1    21 2009      5 0.3125000
4   2     5 2011      5        NA
5   2    10 2012      5 1.0000000
6   2    26 2013     16 1.6000000
7   3     2 2004      2        NA
8   3    12 2004     10 5.0000000
9   3    26 2006     14 1.1666667
10  4     9 2010      9        NA
11  4    16 2011      7 0.7777778
12  4    26 2012     10 0.6250000
13  5     2 2014      2        NA
14  5    15 2015     13 6.5000000
15  5    29 2016     14 0.9333333

If we can assume your data frame is sorted, and that we know how big each group (by id) is, we don't even need to do the whole split-apply thing:

df$ratio2 <- with(df,c(NA,tail(actual,-1) / head(value,-1)))
df$ratio2[seq(1,nrow(df),by = 3)] <- NA
joran
  • 169,992
  • 32
  • 429
  • 468