r - How to count changes in a column -


i searching simple way count changes occurring in column "aa" of data frame; new column "bb" should in df given below:

df <- as.data.frame(cbind("year"=c(2000,2000,2000,2001,2001,2001,2002,2002,2002,2003), "aa"=c(136,137,137,158,162,21,21,55,55,55), "bb"=c(1,2,2,3,4,5,5,6,6,6)))

in other words, change (increase or decrease) in "aa" should accounted in "bb" adding up...

i rle this:

foo <- rle(df$aa) foo$values <- 1:length(foo$values) df$bb <- inverse.rle(foo) 

Comments