【问题标题】:R: Order by Date (by Year, by Month) [duplicate]R:按日期排序(按年,按月)[重复]
【发布时间】:2018-06-01 12:59:19
【问题描述】:

我在第 1 列中有格式为 mm/yyyy 的日期,然后在第 2 列中得到结果。

  1. 月结果
  2. 01/2018 96.13636
  3. 02/2018 96.40000
  4. 3/2018 94.00000
  5. 04/2018 97.92857
  6. 05/2018 95.75000
  7. 11/2017 98.66667
  8. 12/2017 97.78947

如何按月订购,以便从第一个月 (11/2017) 开始到结束 (05/2018)。

我尝试了一些“订单”,但似乎没有一个是按年订购,然后按月订购

【问题讨论】:

    标签: r


    【解决方案1】:

    在 tidyverse 中(添加了 lubridate):

    library(tidyverse)
    library(lubridate)
    
    dfYrMon <- 
        df1 %>% 
        mutate(date = parse_date_time(month, "my"),
               year = year(date),
               month = month(date)
               ) %>% 
        arrange(year, month) %>% 
        select(date, year, month, result)
    

    有数据:

    df1 <- tibble(month = c("01/2018", "02/2018", "03/2018", "04/2018", "05/2018", "11/2017", "12/2017"), 
                  result = c(96.13636, 96.4, 94, 97.92857, 95.75, 98.66667, 97.78947))
    

    将为您提供这个“数据框”:

    # A tibble: 7 x 4
            date  year month   result
          <dttm> <dbl> <dbl>    <dbl>
    1 2017-11-01  2017    11 98.66667
    2 2017-12-01  2017    12 97.78947
    3 2018-01-01  2018     1 96.13636
    4 2018-02-01  2018     2 96.40000
    5 2018-03-01  2018     3 94.00000
    6 2018-04-01  2018     4 97.92857
    7 2018-05-01  2018     5 95.75000
    

    使您的数据值原子化(年份在自己的列中,月份在自己的列中)通常会提高操作的便利性。

    或者,如果您想使用基本 R 日期操作而不是 lubridate

    library(tidyverse)
    
    dfYrMon_base <- 
        df1 %>% 
        mutate(date = as.Date(paste("01/", month, sep = ""), "%d/%m/%Y"),
               year = format(as.Date(date, format="%d/%m/%Y"),"%Y"),
               month = format(as.Date(date, format="%d/%m/%Y"),"%m")
              ) %>%
        arrange(year, month) %>%
        select(date, year, month, result)
    
    dfYrMon_base
    

    注意创建的数据类型。

    # A tibble: 7 x 4
            date  year month   result
          <date> <chr> <chr>    <dbl>
    1 2017-11-01  2017    11 98.66667
    2 2017-12-01  2017    12 97.78947
    3 2018-01-01  2018    01 96.13636
    4 2018-02-01  2018    02 96.40000
    5 2018-03-01  2018    03 94.00000
    6 2018-04-01  2018    04 97.92857
    7 2018-05-01  2018    05 95.75000
    

    【讨论】:

      【解决方案2】:

      我们可以将其转换为yearmon 类,然后执行order

      library(zoo)
      out <- df1[order(as.yearmon(df1$month, "%m/%Y"), df1$Result),]
      row.names(out) <- NULL
      out
      #    month   Result
      #1 11/2017 98.66667
      #2 12/2017 97.78947
      #3 01/2018 96.13636
      #4 02/2018 96.40000
      #5 03/2018 94.00000
      #6 04/2018 97.92857
      #7 05/2018 95.75000
      

      数据

      df1 <- structure(list(month = c("01/2018", "02/2018", "03/2018", "04/2018", 
      "05/2018", "11/2017", "12/2017"), Result = c(96.13636, 96.4, 
      94, 97.92857, 95.75, 98.66667, 97.78947)), .Names = c("month", 
      "Result"), class = "data.frame", 
      row.names = c("1", "2", "3", 
      "4", "5", "6", "7"))
      

      【讨论】:

      • 或者z &lt;- read.zoo(df1, FUN = as.yearmon, format = "%m/%Y"); replace(df1, TRUE, fortify.zoo(z))(如果一个动物园对象就足够了,你可以使用z并省略第二行。)
      • 谢谢!另一个 q... 现在绘制这个我得到错误 在 plot.window(...) 中的错误:需要有限的 'xlim' 值 另外:警告消息:1:在 xy.coords(x, y, xlabel, ylabel, log) :由强制 2 引入的 NA:在 min(x) 中:min 没有非缺失参数;返回 Inf 3:在 max(x) 中:max 没有非缺失参数;返回 -Inf 我不明白,因为 x on y 应该没问题...
      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多