r问题从一列中的日期获得最小和最大

发布于 2025-02-08 13:14:10 字数 1179 浏览 3 评论 0原文

我试图从一组列中的一组日期中获取最小和最大。 当我运行代码(下图)时,我会在查找最小值时获得第一个条目的最大日期(其余的还可以),而在查看最大值时,第一个条目的最大条目的最小值(其余的还可以数据

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2025-02-15 13:14:10

您的date.received是一个因素而不是日期对象
所以尝试:

R_Date.Test$Date.received <-  as.Date(df$Date.received , "%d/%m/%Y")

# then apply your code above

R_Date.Test %>% 
    group_by(PR.Number) %>% 
    slice_min(Date.received)

Your Date.received is a factor not a Date object
so try :

R_Date.Test$Date.received <-  as.Date(df$Date.received , "%d/%m/%Y")

# then apply your code above

R_Date.Test %>% 
    group_by(PR.Number) %>% 
    slice_min(Date.received)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文