R-计算相邻重复项 [英] R - counting adjacent duplicate items
本文介绍了R-计算相邻重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
R的新手,并希望执行以下操作:我有一组数字,例如(1,1,0,1,1,1,0,0,1),并且需要在相邻重复出现时对其进行计数.我正在寻找的结果是:
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
2,1,3,2,1
如2个,1个零,3个等...
as in 2 ones, 1 zero, 3 ones, etc.
谢谢.
推荐答案
我们可以使用 rle
rle(v1)$lengths
#[1] 2 1 3 2 1
数据
v1 <- c(1,1,0,1,1,1,0,0,1)
这篇关于R-计算相邻重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文