lubridate包的week和month函数的输出如何modify/addyear?

How to modify/add year to the output of the week and month function of lubridate package?

我正在尝试获取周数。当我只有一年的数据时,lubridatemonth 函数可以完成这项工作。但是当我有超过一年的数据时,问题就出现了。
例如:

library(lubridate)
data$weeks <- week(data$Date)
data$months <- months(data$Date)

我的数据现在看起来像:

       Date    weeks    months
1   2017-01-01     1   January
2   2017-01-02     1   January
6   2017-01-06     1   January
7   2017-01-07     1   January
9   2018-01-09     2   January
10  2018-01-10     2   January
11  2018-01-11     2   January
12  2018-01-12     2   January

区分年份的预期输出(如下所示):

       Date        weeks       months
1   2017-01-01     1-2017   January-2017
2   2017-01-02     1-2017   January-2017
6   2017-01-06     1-2017   January-2017
7   2017-01-07     2-2017   January-2017
9   2018-01-09     2-2018   January-2018
10  2018-01-10     2-2018   January-2018
11  2018-01-11     2-2018   January-2018
12  2018-01-12     2-2018   January-2018

我也想为 quarter 功能做点什么。

我不确定这是否是您要查找的内容,但您可以使用 paste0,并且正如您提到的,lubridate::year:

library(lubridate)

df1 %>% 
  mutate(weeks = paste0(weeks, "-", year(Date)),
         months = paste0(months, "-", year(Date)))

#        Date  weeks       months
#1 2017-01-01 1-2017 January-2017
#2 2017-01-02 1-2017 January-2017
#3 2017-01-06 1-2017 January-2017
#4 2017-01-07 1-2017 January-2017
#5 2018-01-09 2-2018 January-2018
#6 2018-01-10 2-2018 January-2018
#7 2018-01-11 2-2018 January-2018
#8 2018-01-12 2-2018 January-2018

要添加宿舍,您可以执行以下操作:

df1 %>%  
  mutate(weeks = paste0(weeks, "-", year(Date)),
         months = paste0(months, "-", year(Date))) %>% 
  mutate(quarters = paste0(quarter(Date), "-", year(Date)))

#        Date  weeks       months quarters
#1 2017-01-01 1-2017 January-2017   1-2017
#2 2017-01-02 1-2017 January-2017   1-2017
#3 2017-01-06 1-2017 January-2017   1-2017
#4 2017-01-07 1-2017 January-2017   1-2017
#5 2018-01-09 2-2018 January-2018   1-2018
#6 2018-01-10 2-2018 January-2018   1-2018
#7 2018-01-11 2-2018 January-2018   1-2018
#8 2018-01-12 2-2018 January-2018   1-2018

数据:

structure(list(Date = structure(c(17167, 17168, 17172, 17173, 
17540, 17541, 17542, 17543), class = "Date"), weeks = c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L), months = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = "January", class = "factor")), class = "data.frame", row.names = c(NA, 
-8L))

‍‍‍‍‍‍‍‍lubridate::week() 仅 returns 日期时间对象的星期部分。如果您想将 lubridate::week 附加到 lubridate::year,请执行以下操作:

library(tidyverse)

df <- data.frame(
  Date = as.Date(c(
      "2017-01-01", "2017-01-02", "2017-01-06", "2017-01-07", "2018-01-09",
      "2018-01-10", "2018-01-11", "2018-01-12"
      ))
)

df %>% 
  mutate(
    weeks = paste0(lubridate::week(Date), "-", lubridate::year(Date)),
    months = paste0(base::months(Date), "-", lubridate::year(Date))
    ) -> df

df

        Date  weeks       months
1 2017-01-01 1-2017 January-2017
2 2017-01-02 1-2017 January-2017
3 2017-01-06 1-2017 January-2017
4 2017-01-07 1-2017 January-2017
5 2018-01-09 2-2018 January-2018
6 2018-01-10 2-2018 January-2018
7 2018-01-11 2-2018 January-2018
8 2018-01-12 2-2018 January-2018

您可以使用基数 R 从 Date 中获取星期和月份名称。阅读?strptime。要添加季度,我们可以使用基础 R quarters 函数和 paste 它与年份。

df$weeks <- format(df$Date, "%U-%Y")
df$months <- format(df$Date, "%B-%Y")
df$quarters <- paste(quarters(df$Date), format(df$Date, "%Y"), sep = "-")

df
#         Date   weeks       months quarters
#1  2017-01-01 01-2017 January-2017 Q1-2017
#2  2017-01-02 01-2017 January-2017 Q1-2017
#6  2017-01-06 01-2017 January-2017 Q1-2017
#7  2017-01-07 01-2017 January-2017 Q1-2017
#9  2018-01-09 01-2018 January-2018 Q1-2018
#10 2018-01-10 01-2018 January-2018 Q1-2018
#11 2018-01-11 01-2018 January-2018 Q1-2018
#12 2018-01-12 01-2018 January-2018 Q1-2018

数据

df <- structure(list(Date = structure(c(17167, 17168, 17172, 17173, 
17540, 17541, 17542, 17543), class = "Date"), weeks = c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L), months = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = "January", class = "factor")), row.names = c("1", 
"2", "6", "7", "9", "10", "11", "12"), class = "data.frame")