Fra*_*cis 10 indexing r duplicates
我想删除重复的但保留第一个二进制向量:
x = c(0,0,1,1,0,1,0,1,1,1,0,1) # the input
y = c(0,0,1,0,1,0,1,0,1) # the desired output
Run Code Online (Sandbox Code Playgroud)
即,分别移除第一组和第三组中的一个1和两个,并保留该组中的第一组.11
我想用rle用cumsum,但尚未计算出来.任何建议将不胜感激.
使用rle/inverse.rle
res <- rle(x)
res$lengths[res$values == 1] <- 1
inverse.rle(res)
## [1] 0 0 1 0 1 0 1 0 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
428 次 |
| 最近记录: |