【问题标题】:Ordering dates in R with lubridate使用 lubridate 在 R 中订购日期
【发布时间】:2021-06-01 09:18:13
【问题描述】:

我有以下数据框,想订购。

time <- c ("Feb (2019)", "Apr (2019)", "Jun (2019)" ,"Aug (2019)", "Oct (2019)", "Dec (2019)", "Feb (2020)" "Apr (2020)", "Jun (2020)", "Aug (2020)", "Oct (2020)", "Dec (2020)", "Jan (2019)", "Mar (2019)", "May (2019)", "Jul (2019)", "Sep (2019)", "Nov (2019)", "Jan (2020)", "Mar (2020)", "May (2020)", "Jul (2020)" "Sep (2020)", "Nov (2020)", "Jan (2021)")

为此,我使用以下代码

library(lubridate)

time <- gsub("[()]", "", time)
time <- (my(time))
time <- as.data.frame(as.integer(gsub("[-]", "", time)))
arrange(time)

很遗憾,我没有得到我想要的结果:

1                           20190201
2                           20190401
3                           20190601
4                           20190801
5                           20191001
6                           20191201
7                           20200201
8                           20200401
9                           20200601
10                          20200801
11                          20201001
12                          20201201
13                          20190101
14                          20190301
15                          20190501
16                          20190701
17                          20190901
18                          20191101
19                          20200101
20                          20200301
21                          20200501
22                          20200701
23                          20200901
24                          20201101
25                          20210101

我尝试了各种方法,但没有任何效果。非常感谢您的建议!

【问题讨论】:

  • sort(strptime(sprintf("01 %s",time), "%d %b (%Y)"))

标签: r lubridate


【解决方案1】:

您可以按如下方式重新排列日期:

library(dplyr)
library(readr)

time <- c("Feb (2019)", "Apr (2019)", "Jun (2019)", "Aug (2019)", "Oct (2019)", 
          "Dec (2019)", "Feb (2020)", "Apr (2020)", "Jun (2020)", "Aug (2020)", 
          "Oct (2020)", "Dec (2020)", "Jan (2019)", "Mar (2019)", "May (2019)", 
          "Jul (2019)", "Sep (2019)", "Nov (2019)", "Jan (2020)", "Mar (2020)", 
          "May (2020)", "Jul (2020)", "Sep (2020)", "Nov (2020)", "Jan (2021)")

tibble(time = time) %>% 
  arrange(parse_date(time, "%b (%Y)"))
#> # A tibble: 25 x 1
#>    time      
#>    <chr>     
#>  1 Jan (2019)
#>  2 Feb (2019)
#>  3 Mar (2019)
#>  4 Apr (2019)
#>  5 May (2019)
#>  6 Jun (2019)
#>  7 Jul (2019)
#>  8 Aug (2019)
#>  9 Sep (2019)
#> 10 Oct (2019)
#> # ... with 15 more rows

如果你想保留解析的时间,你可以调用mutate(),例如

tibble(time = time) %>% 
  mutate(parsed_time = parse_date(time, "%b (%Y)"))
  arrange(parsed_time)

【讨论】:

    【解决方案2】:

    你也可以使用zoo::as.yearmon

    time[order(zoo::as.yearmon(time, '%b (%Y)'))]
    
    # [1] "Jan (2019)" "Feb (2019)" "Mar (2019)" "Apr (2019)" "May (2019)" "Jun (2019)"
    # [7] "Jul (2019)" "Aug (2019)" "Sep (2019)" "Oct (2019)" "Nov (2019)" "Dec (2019)"
    #[13] "Jan (2020)" "Feb (2020)" "Mar (2020)" "Apr (2020)" "May (2020)" "Jun (2020)"
    #[19] "Jul (2020)" "Aug (2020)" "Sep (2020)" "Oct (2020)" "Nov (2020)" "Dec (2020)"
    #[25] "Jan (2021)"
    

    【讨论】:

      猜你喜欢
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      相关资源
      最近更新 更多