转换为列表时格式化日期
我有这样的桌子:
ID | 日期 | 状态 |
---|---|---|
101 | 2020-09-14 | 1 |
102 | 2020-09-14 | 1 |
103 | 2020-09-14 | 1 |
104 | 2020-09-14 | 2 |
105 | 2020-09-14 | 2 |
106 | 2020-09-- 14 | 2 |
但想要这样的桌子:
状态 | ID | 日期 |
---|---|---|
1 | 101,102,103 | 2020-09-14,2020-09-14,2020-09-14 |
1 | 104,105,106 | 2020-09-14,2020-09-14,2020-09-14,2020-09-14 |
我目前正在使用的代码:
注意:日期在运行代码之前以YYYY-MM-DD格式使用。
g1 <- df1 %>%
mutate(Date = as.Date(Date, format = '%Y%m%d')) %>%
group_by(status) %>%
summarise_at(c("ID", "Date"), list)
除了新表中的日期不在yyyy-mm-dd中,这似乎有效。例如,2021-06-10正在转换为18788。
I have a table like this:
ID | Date | Status |
---|---|---|
101 | 2020-09-14 | 1 |
102 | 2020-09-14 | 1 |
103 | 2020-09-14 | 1 |
104 | 2020-09-14 | 2 |
105 | 2020-09-14 | 2 |
106 | 2020-09-14 | 2 |
But want a table like this:
Status | ID | Date |
---|---|---|
1 | 101,102,103 | 2020-09-14, 2020-09-14, 2020-09-14 |
1 | 104,105,106 | 2020-09-14, 2020-09-14, 2020-09-14 |
Code that i'm currently using:
note: date is in format yyyy-mm-dd before running code.
g1 <- df1 %>%
mutate(Date = as.Date(Date, format = '%Y%m%d')) %>%
group_by(status) %>%
summarise_at(c("ID", "Date"), list)
This seems to work except for the date in the new table is not in yyyy-mm-dd. For example, 2021-06-10 is converting to 18788.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能的解决方案:
一个更简洁的替代方案:
A possible solution:
A more succinct alternative: