如何使用R中的跳过规则为最低和最高日期创建指标变量?
我想要一个指标变量,它告诉我日期是按ID组的最低还是最高。但是,我不希望它将任何具有练习的东西都计数。这就是数据框架现在的外观以及我想要的外观。
ID | 日期 | 练习 |
---|---|---|
1 | 02-27-2020 | 1 |
1 | 04-21-2021 | 0 |
1 | 06-24-2022 | 0 |
2 | 03-21-2019 | 0 |
2 | 09-19-2020 | 0 |
2 | 01-21-2021 | 0 |
结束产品:
ID | 日期 | 练习 | 最低 | 03-21-2019 |
---|---|---|---|---|
1 | 02-27-2020 | 1 | 0 | 0 |
1 | 04-21-2021 | 0 | 1 | 0 |
1 0 1 | 06-24-2022 | 0 | 0 | 1 |
2 | 0 | 0 | 1 | 0 |
2 | 09-19-9-2020 | 0 | 0 | 0 |
2 | 01 -21-2021 | 0 | 0 | 1 |
I want an indicator variable that tells me if the date is the lowest or highest when its group by ID. However, I don't want it to count anything that has practice as 1. This is what the data frame looks now and what I want it to look like.
ID | date | practice |
---|---|---|
1 | 02-27-2020 | 1 |
1 | 04-21-2021 | 0 |
1 | 06-24-2022 | 0 |
2 | 03-21-2019 | 0 |
2 | 09-19-2020 | 0 |
2 | 01-21-2021 | 0 |
End product:
ID | date | practice | lowest | highest |
---|---|---|---|---|
1 | 02-27-2020 | 1 | 0 | 0 |
1 | 04-21-2021 | 0 | 1 | 0 |
1 | 06-24-2022 | 0 | 0 | 1 |
2 | 03-21-2019 | 0 | 1 | 0 |
2 | 09-19-2020 | 0 | 0 | 0 |
2 | 01-21-2021 | 0 | 0 | 1 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该代码使用
tidyverse
提供了所需的解决方案。请注意,我必须将日期
强制为日期格式;默认情况下,mm-dd-yyyy格式被读取为字符,这会导致min(date)
andmax(date)
读取不同的值,为min和max。This code delivered the desired solution using the
tidyverse
. Note that I had to forcedate
into a date format; by default the mm-dd-YYYY format gets read as character, which causesmin(date)
andmax(date)
to read different values as min and max.这是带有
AVE
的基本R解决方案。由
Here is a base R solution with
ave
.Created on 2022-04-25 by the reprex package (v2.0.1)
数据。表方法:
输出:
输入:
A data.table approach:
Output:
Input: