[R] sort of cumulative counting in a vector

Stefan Uhmann stefan.uhmann at googlemail.com
Thu Nov 5 14:30:36 CET 2009


Dear list,

I need help, since I can not come up with an easy solution to convert 
this vector

test <- c('p','p','t','t','t')

to

[1] NA NA  1  2  3

which means the occurences of 't' should be summed up at the 
corresponding positions. The solution should also be able to handle the 
following scenarios:

test2 <- c('t','t','t')

[1] 1  2  3

test3 <- c('p','p')

[1] NA NA

test <- c('p','p','t','t','t','p','k','t')

[1] NA NA  1  2  3 NA NA 4


I would really appreciate an easy solution!

Thanks,
Stefan




More information about the R-help mailing list