R - counting adjacent duplicate items

对着背影说爱祢 提交于 2021-02-05 12:15:15

问题


New to R and would like to do the following operation: I have a set of numbers e.g. (1,1,0,1,1,1,0,0,1) and need to count adjacent duplicates as they occur. The result I am looking for is:

2,1,3,2,1

as in 2 ones, 1 zero, 3 ones, etc.

Thanks.


回答1:


We can use rle

rle(v1)$lengths
#[1] 2 1 3 2 1

data

v1 <- c(1,1,0,1,1,1,0,0,1) 


来源:https://stackoverflow.com/questions/33528106/r-counting-adjacent-duplicate-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!