简体   繁体   English

如何在lubridate软件包的星期和月功能的输出中修改/添加年份?

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

I'm trying to get week numbers. 我正在尝试获取周数。 lubridate 's month function does the job, when i only one year data. 当我只有一年的数据时, lubridatemonth功能可以完成工作。 But problem arises when I have more than one year of data. 但是,当我拥有超过一年的数据时,就会出现问题。
Ex: 例如:

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

my data now looks like: 我的数据现在看起来像:

       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

expected output(something like below) that distinguishes years: 可以区分年份的预期输出(如下所示):

       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

I want to do something for quarter function also. 我也想为quarter功能做些事情。

I'm not sure if this is what you're looking for, but you can use paste0 and, as you mentioned, lubridate::year : 我不确定这是否是您要寻找的东西,但是您可以使用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

To add quarters you can do: 要添加季度,您可以执行以下操作:

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

The data: 数据:

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))

You could use base R to get week and month name from Date . 您可以使用基数R从Date获取星期和月份的名称。 Read ?strptime . 阅读?strptime To add quarters, we can use base R quarters function and paste it with year. 要添加四分之一,我们可以使用基本R quarters函数pastepaste到年份中。

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

data 数据

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")

‍‍‍‍‍‍‍‍ lubridate::week() only returns week component of a date-time object. lubridate::week()只返回日期时间目标星期的组成部分。 if you want to attach lubridate::week to lubridate::year do as follows: 如果要将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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM