通过改变因子值的R标签序列
我有以下数据:
帐户 | 日期 | 类型 |
---|---|---|
1 | 2021-08-31 | 0 |
1 | 2021-09-23 | 0 |
1 | 2021-09-30 | 5 |
1 | 2021-10-30 | 0 |
1 | 2021-12-29 | 0 |
1 | 2022-01-31 | 8 |
1 | 2022-02-02 | 0 |
我需要找到每个单独转换的最短日期。
group_by(Account, type) %>%
summarise(first_appearance = min(date))
返回
帐户 | 日期 | 类型 |
---|---|---|
1 | 2021-08-31 | 0 |
1 | 2021-09-30 | 5 |
1 | 2022-01-31 | 8 |
如何按类型中的每个 SHIFT 进行分组?
我最初的想法是沿着因子生成某种序列并连接起来以获得唯一的分组变量,但是这是如何完成的呢?
账户 | 日期 | 类型 | 订单 | type_order |
---|---|---|---|---|
1 | 2021-08-31 | 0 | A | 0A |
1 | 2021-09-23 | 0 | A | 0A |
1 | 2021-09-30 | 5 | A | 5A |
1 | 2021-10-30 | 0 | B | 0B |
1 | 2021-12-29 | 0 | B | 0B |
1 | 2022-01-31 | 8 | A | 8A |
1 | 2022-02-02 | 0 | C | 0C |
所需的输出为:
group_by(Account, type_order) %>%
summarise(first_appearance = min(date))
Account | date | type | order | type_order |
---|---|---|---|---|
1 | 2021-08-31 | 0 | A | 0A |
1 | 2021-09-30 | 5 | A | 5A |
1 | 2021-10-30 | 0 | B | 0B |
1 | 2022 -01-31 | 8A | 8A | |
1 | 2022年2月2日 | 0 | ℃ | 0 ℃ |
I have the following data:
Account | date | type |
---|---|---|
1 | 2021-08-31 | 0 |
1 | 2021-09-23 | 0 |
1 | 2021-09-30 | 5 |
1 | 2021-10-30 | 0 |
1 | 2021-12-29 | 0 |
1 | 2022-01-31 | 8 |
1 | 2022-02-02 | 0 |
I need to find the minimum date of each individual transition.
group_by(Account, type) %>%
summarise(first_appearance = min(date))
returns
Account | date | type |
---|---|---|
1 | 2021-08-31 | 0 |
1 | 2021-09-30 | 5 |
1 | 2022-01-31 | 8 |
How can I group by each SHIFT in type?
My initial thoughts are to generate some sort of sequence along the factors and concatenate to have a unique grouping variable, but how would this be done?
Account | date | type | order | type_order |
---|---|---|---|---|
1 | 2021-08-31 | 0 | A | 0A |
1 | 2021-09-23 | 0 | A | 0A |
1 | 2021-09-30 | 5 | A | 5A |
1 | 2021-10-30 | 0 | B | 0B |
1 | 2021-12-29 | 0 | B | 0B |
1 | 2022-01-31 | 8 | A | 8A |
1 | 2022-02-02 | 0 | C | 0C |
Desired output would be:
group_by(Account, type_order) %>%
summarise(first_appearance = min(date))
Account | date | type | order | type_order |
---|---|---|---|---|
1 | 2021-08-31 | 0 | A | 0A |
1 | 2021-09-30 | 5 | A | 5A |
1 | 2021-10-30 | 0 | B | 0B |
1 | 2022-01-31 | 8 | A | 8A |
1 | 2022-02-02 | 0 | C | 0C |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当行与行之间的类型存在差异时,可以使用 data.table 中的 rleid 来分配组。
输出
Maybe use
rleid
fromdata.table
to assign groups when there are differences intype
from row to row.Output