如何计算行(R)中的变化数量
df <- data.frame(Num1=c(1,0,1,0,1), Num2=c(0,1,1,0,1), Num3=c(1,1,1,1,1), Num4=c(1,1,0,0,1), Num5=c(1,1,1,0,0))
我需要计算R中每一行的价值变化多少次。 谢谢你!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
即第一行是从1到0的更改,然后返回1。因此总共2个更改等
ie first row there is a change from 1 to 0 then back to 1. so a total of 2 changes etc
这是一个尝试,
由
Here is a try ,
Created on 2022-06-02 by the reprex package (v2.0.1)
我们可以使用
应用
(margin = 1
)循环循环,然后使用rle
(运行长度编码),检查长度
We may loop over the row with
apply
(MARGIN=1
), then userle
(run-length-encoding), check thelengths