Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

R increment by 1 for every change in value column and restart the counter

$
0
0

I would like to find a way to do very similar to this question. Increment by 1 for every change in column

But i want to restart the counter when var1 = c using df$var2 <- with(rle(as.character(df$var1)), rep(seq_along(values), lengths))*

results in column var 2

var1 var2 Should be
   a    1   1
   a    1   1
   1    2   2
   0    3   3
   b    4   4
   b    4   4
   b    4   4
   c    5   1
   1    6   2
   1    6   2

Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>