使用r来计算二进制输出= 1以来的时间
我在具有时间功能的数据范围内有二进制数据,我希望在下面的“ = 1”以来的新列产生类似的数据框架。我能够找到相当于这个答案的python 。我正在寻找一种在r中做到这一点的方法
Binary Output Time (secs) duration since =1
0 0 0
0 0.000983 0.000983
0 0.001966 0.001966
1 0.002949 0
0 0.003932 0.000983 # (0.003932-0.002949)
0 0.005000 0.002051 # (0.005000-0.002949)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们可以使用
cumsum
指示我们是否应该使用time
使用binary_output
== 1。如果cumsum
== 0 ,这意味着所有以前的binary_output
的值为0,并且我们不会在这些行中使用binarts_output
== 1减去time
。由
We can use
cumsum
to indicate whether we should subtractTime
withBinary_Output
== 1. Ifcumsum
== 0, it means all previousBinary_Output
has a value of 0, and we will not subtractTime
withBinary_Output
== 1 in these rows.Created on 2022-05-05 by the reprex package (v2.0.1)
使用
data.table
:With
data.table
:另外,这可以通过在
cumsum(binary_output)上进行分组来解决,该
有益于复制第一组的OP的预期结果,即前3行:数据
Alternatively, this can be solved by grouping on
cumsum(Binary_Output)
which has the benefit to reproduce OP's expected result for the first group, i.e., the first 3 rows:Data