r问题从一列中的日期获得最小和最大
我试图从一组列中的一组日期中获取最小和最大。 当我运行代码(下图)时,我会在查找最小值时获得第一个条目的最大日期(其余的还可以),而在查看最大值时,第一个条目的最大条目的最小值(其余的还可以数据
:
structure(list(PR.Number = structure(c(1L, 1L, 2L, 2L, 3L, 3L
), .Label = c("PR1234", "PR1235", "PR1236", "PR1237", "PR1238"
), class = "factor"), Date.received = structure(c(2L, 1L, 7L,
10L, 8L, 9L), .Label = c("01/02/2024", "05/01/2022", "09/09/2023",
"09/09/2025", "10/03/2023", "10/03/2024", "15/07/2022", "16/11/2022",
"16/11/2023", "30/09/2022"), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
代码最大:
R_Date.Test %>%
group_by(PR.Number) %>%
slice_max(Date.received)
最小值:
R_Date.Test %>%
group_by(PR.Number) %>%
slice_min(Date.received)
输出 Pr.Number Date.Received
1 Pr1234 01/02/2024<这是错误的05/01/2022
2 PR1235 15/07/2022
3 PR1236 16/11/2022
4 PR1237 10/03/2023
5 PR1237 10/03/2023
6 PR1238 09/09/2023
最大输出: Pr.Number Date.Received
1 Pr1234 05/01/2022<这是错误的01/02/2024
2 PR1235 30/09/2022
3 PR1236 16/11/2023
4 PR1237 10/03/2024
5 PR1238 09/09/2025
I am trying to get the Min and Max from a group of dates in a column.
When I run my code (below) I get the Max date for the first entry when looking for the min (the rest are ok), and the min for the max entry for the first entry when looking at the max (the rest are ok.
Data:
structure(list(PR.Number = structure(c(1L, 1L, 2L, 2L, 3L, 3L
), .Label = c("PR1234", "PR1235", "PR1236", "PR1237", "PR1238"
), class = "factor"), Date.received = structure(c(2L, 1L, 7L,
10L, 8L, 9L), .Label = c("01/02/2024", "05/01/2022", "09/09/2023",
"09/09/2025", "10/03/2023", "10/03/2024", "15/07/2022", "16/11/2022",
"16/11/2023", "30/09/2022"), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
Code Max:
R_Date.Test %>%
group_by(PR.Number) %>%
slice_max(Date.received)
Code Min:
R_Date.Test %>%
group_by(PR.Number) %>%
slice_min(Date.received)
Min Output:
PR.Number Date.received
1 PR1234 01/02/2024 < this is wrong 05/01/2022
2 PR1235 15/07/2022
3 PR1236 16/11/2022
4 PR1237 10/03/2023
5 PR1237 10/03/2023
6 PR1238 09/09/2023
Max Output:
PR.Number Date.received
1 PR1234 05/01/2022 < this is wrong 01/02/2024
2 PR1235 30/09/2022
3 PR1236 16/11/2023
4 PR1237 10/03/2024
5 PR1238 09/09/2025
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
date.received
是一个因素而不是日期对象所以尝试:
Your
Date.received
is a factor not a Date objectso try :