【问题标题】:labeling week dates just by the month R仅按 R 月标记周日期
【发布时间】:2024-01-11 08:32:01
【问题描述】:

我有一个df如下:

x = data.frame(retailer = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
store = c(5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6),
week = c(2021100301, 2021092601, 2021091901, 2021091201, 2020082901, 2020082201, 2020081501, 2020080801, 2021080101, 2021072501, 2021071801,
  2021071101, 2020070401, 2020062701, 2020062001, 2020061301))

我有几周的值对应于日期,例如 2021100301 是 2021 年 10 月 3 日,2021071101 是 2020 年 7 月 11 日。我想添加一个额外的列,我可以在其中以 2021100301 为 10 的格式标记这些周,因为它是 10 月,而 2021090301 将是 09,因为它是 9 月,依此类推。我想在列中的所有星期都这样做,基本上只是在单独的列中按月标记它们。我试图让输出为:

x = data.frame(retailer = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
store = c(5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6),
week = c(2021100301, 2021092601, 2021091901, 2021091201, 2020082901, 2020082201, 2020081501, 2020080801, 2021080101, 2021072501, 2021071801,
  2021071101, 2020070401, 2020062701, 2020062001, 2020061301),
month = c(10, 09, 09, 09, 08, 08, 08, 08, 08, 07, 07, 07, 07, 06, 06 ,06))


有什么办法可以做到这一点吗?谢谢。

【问题讨论】:

    标签: r dataframe date label


    【解决方案1】:

    试试substr

    library(dplyr)
    x %>% 
       mutate(month = as.numeric(substr(week, 5, 6)))
    

    -输出

     retailer store       week month
    1         2     5 2021100301    10
    2         2     5 2021092601     9
    3         2     5 2021091901     9
    4         2     5 2021091201     9
    5         2     5 2020082901     8
    6         2     5 2020082201     8
    7         2     5 2020081501     8
    8         2     5 2020080801     8
    9         2     6 2021080101     8
    10        2     6 2021072501     7
    11        2     6 2021071801     7
    12        2     6 2021071101     7
    13        2     6 2020070401     7
    14        2     6 2020062701     6
    15        2     6 2020062001     6
    16        2     6 2020061301     6
    

    【讨论】:

    • 谢谢!如果我想包括年份的最后两位数字来区分月份发生在哪一年,我必须这样做 as.numeric(substr(week, 3, 4, 5, 6)) 吗?还是有其他方法可以做到这一点?
    • @JaneMiller 应该是substr(week, 3, 6)
    【解决方案2】:

    这是另一种方法: 逻辑:

    1. 去掉最后两位数
    2. 转换为上课日期
    3. 获取月份
    library(dplyr)
    library(lubridate)
    x %>% 
      mutate(month1 = gsub('.{2}$', '', week),
             month1 = month(ymd(month1)))
    
       retailer store       week month1
    1         2     5 2021100301     10
    2         2     5 2021092601      9
    3         2     5 2021091901      9
    4         2     5 2021091201      9
    5         2     5 2020082901      8
    6         2     5 2020082201      8
    7         2     5 2020081501      8
    8         2     5 2020080801      8
    9         2     6 2021080101      8
    10        2     6 2021072501      7
    11        2     6 2021071801      7
    12        2     6 2021071101      7
    13        2     6 2020070401      7
    14        2     6 2020062701      6
    15        2     6 2020062001      6
    16        2     6 2020061301      6
    

    【讨论】: