dataframe - I want to calculate a moving difference for my dataset below. -
how add column moving difference of column2?
for example: want add column have following values: (0,-372706.6,-284087.1, -119883.7, etc.)
here's way go it.
## small dataset x <- data.frame(matrix(nrow=7,ncol=2,c(0,12,1,10,2,9.5,3,8,4,7,5,5,6,2),byrow = t)) names(x) <- c("time","count") x[1,"diff"] <- na x[2:nrow(x),"diff"] <- rev(diff(rev(x$count)))
there way plyr
package well.
Comments
Post a Comment