find-occurrences

R: count consecutive occurrences of values in a single column

社会主义新天地 提交于 2019-11-26 12:21:24
I wish to create a sequential number within each run of equal values, like a counter of occurrences, which restarts once the value in the current row is different from the previous row. Please find an example of input and expected output below. dataset <- data.frame(input = c("a","b","b","a","a","c","a","a","a","a","b","c")) dataset$counter <- c(1,1,2,1,2,1,1,2,3,4,1,1) dataset # input counter # 1 a 1 # 2 b 1 # 3 b 2 # 4 a 1 # 5 a 2 # 6 c 1 # 7 a 1 # 8 a 2 # 9 a 3 # 10 a 4 # 11 b 1 # 12 c 1 My question is very similar to this one: Cumulative sequence of occurrences of values . You need to use

R: count consecutive occurrences of values in a single column

烂漫一生 提交于 2019-11-26 02:36:03
问题 I wish to create a sequential number within each run of equal values, like a counter of occurrences, which restarts once the value in the current row is different from the previous row. Please find an example of input and expected output below. dataset <- data.frame(input = c(\"a\",\"b\",\"b\",\"a\",\"a\",\"c\",\"a\",\"a\",\"a\",\"a\",\"b\",\"c\")) dataset$counter <- c(1,1,2,1,2,1,1,2,3,4,1,1) dataset # input counter # 1 a 1 # 2 b 1 # 3 b 2 # 4 a 1 # 5 a 2 # 6 c 1 # 7 a 1 # 8 a 2 # 9 a 3 # 10