每次将另一列的文本更改r恢复为0
在R中,我有一个带有一个时间列的数据框,每当另一列中的文本更改时,我希望将其恢复为一个或零。文本再也没有回到上一段文字。 文本更改的395倍,〜270'000行。
目前表
时间 | 试验 |
---|---|
11 | A |
12 | A |
13 | B |
14 | B |
表想要
时间 | 试验 |
---|---|
1 | A |
2 | A |
2 A 1 | B |
2 | B |
In R, I have a data frame with a time column that I want to revert to one or zero every time the text in another column changes. The text never goes back to the previous text.
There are 395 times the text changes and ~270'000 rows.
Table at the moment
Time | Trial |
---|---|
11 | A |
12 | A |
13 | B |
14 | B |
Table wanted
Time | Trial |
---|---|
1 | A |
2 | A |
1 | B |
2 | B |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)基本解决方案是:
2)另一个基本解决方案是:
3)dplyr 我们可以写:
基准测试
基准基础解决方案问题的数据速度要快得多,但建议您使用数据或其中一部分重复此操作。
注意
以可重复形式输入。
1) A base solution would be:
2) Another base solution would be:
3) dplyr With dplyr we can write:
Benchmark
The base solutions are much faster on the data in the question but suggest you repeat this with your data or a subset of it.
Note
Input in reproducible form.
我们可以分组,然后使用
row_number()
We could group and then use
row_number()