r - Episode count for each row -


i'm sure has been asked before life of me can't figure out search for!

i have following data:

x y 1 3 1 3 1 3 1 2 1 2 2 2 2 4 3 4 3 4 

and output running count resets everytime either x or y changes value.

x y o 1 3 1 1 3 2 1 3 3 1 2 1 1 2 2 2 2 1 2 4 1 3 4 1 3 4 2 

try like

df<-read.table(header=t,text="x y 1 3 1 3 1 3 1 2 1 2 2 2 2 4 3 4 3 4") cbind(df,o=sequence(rle(paste(df$x,df$y))$lengths)) > cbind(df,o=sequence(rle(paste(df$x,df$y))$lengths)) x y o 1 1 3 1 2 1 3 2 3 1 3 3 4 1 2 1 5 1 2 2 6 2 2 1 7 2 4 1 8 3 4 1 9 3 4 2 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -