Question

DF2
Date         EMMI      ACT      NO2
2011/02/12   12345     21       11
2011/02/14   43211     22       12
2011/02/19   12345     21       13
2011/02/23   43211     13       12
2011/02/23   56341     13       12
2011/03/03   56431     18       20 

I need to find difference between two dates in a column. For example difference between ACT column values.For example, the EMMI 12345, Difference between dates 2011/02/19 - 2011/02/12 = 21-21 = 0. like that i want to do for entire column of ACT. Add a new column diff and add values to that. Can anybody let me know please how to do it.

This is the output i want

DF3
Date         EMMI      ACT      NO2  DifACT
2011/02/12   12345     21       11    NA
2011/02/14   43211     22       12    NA
2011/02/19   12345     21       13    0
2011/02/23   43211     13       12    -9
2011/02/23   56341     13       12    5
Was it helpful?

Solution

Try this:

DF3 <- DF2
DF3$difACT <- ave( DF3$ACT, DF3$EMMI, FUN= function(x) c(NA, diff(x)) )

As long as the dates are sorted (within EMMI) this will work, if they are not sorted then we would need to modify the above to sort within EMMI first. I would probably sort the entire data frame on date first (and save the results of order), then run the above. Then if you need it back in the original order you can run order on the results of the original order results to "unorder" the data frame.

OTHER TIPS

This is based on plyr package (not tested):

library(plyr)
DF3<-ddply(DF2,.(EMMI),mutate,difACT=diff(ACT))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top