简体   繁体   English

查找 x 年第 x 周的第一天

[英]Find the first day of the x week of the x year

Is there a way to find the first day of the x week of the x year with the package lubridate?有没有办法用 package lubridate 找到 x 年 x 周的第一天?

For example I have this data: week number in some year (eg 33th week from the year 1988) and i would like to convert it into a data like: day/month/year (eg 15/08/1988).例如,我有这个数据:某年的周数(例如 1988 年的第 33 周),我想将其转换为如下数据:日/月/年(例如 1988 年 8 月 15 日)。

Missing_day <- c("33/1988", "37/1991", "37/1992", 
             "41/1994", "15/1997", "18/2001", 
             "50/2001", "16/2002", "35/2004", 
             "5/2012", "50/2012", "8/2013", 
             "36/2013", "51/2017")

thanks谢谢

You can attach the weekday and convert the data into Date.您可以附加工作日并将数据转换为日期。 Using base R, you can do:使用基础 R,您可以执行以下操作:

as.Date(paste0('1/', Missing_day), '%u/%U/%Y')

# [1] "1988-08-15" "1991-09-16" "1992-09-14" "1994-10-10" "1997-04-14"
# [6] "2001-05-07" "2001-12-17" "2002-04-22" "2004-08-30" "2012-01-30"
#[11] "2012-12-10" "2013-02-25" "2013-09-09" "2017-12-18"

Do note the discussion here about:请注意这里的讨论:

  1. Specifying the day of the week指定星期几
  2. Ensuring you understand the different ways in which R enumerates weeks of a year确保您了解 R 枚举一年中几周的不同方式
library(dplyr)
library(stringr)
# Using Lubridate per your request
library(lubridate)
tibble(week_year = Missing_day) %>%
  mutate(
    day_month_year = format(lubridate::parse_date_time(
      paste(
        str_extract(string = week_year, pattern = "^\\d+"),
        str_extract(string = week_year, pattern = "\\d*$"),
        'Mon',
        sep =
          "/"
      ), 'W/Y/a'
    ), "%d/%m/%Y")
  )

# A tibble: 14 x 2
# week_year  day_month_year
#    <chr>        <chr>         
# 1 33/1988    15/08/1988    
# 2 37/1991    16/09/1991    
# 3 37/1992    14/09/1992    
# 4 41/1994    10/10/1994    
# 5 15/1997    14/04/1997    
# 6 18/2001    30/04/2001    
# 7 50/2001    10/12/2001    
# 8 16/2002    22/04/2002    
# 9 35/2004    30/08/2004    
# 10 5/2012    30/01/2012    
# 11 50/2012   10/12/2012    
# 12 8/2013    25/02/2013    
# 13 36/2013   09/09/2013    
# 14 51/2017   18/12/2017 
# Using Base R
tibble(week_year = Missing_day) %>%
  mutate(
    day_month_year = format(as.Date(
      paste(
        1,
        str_extract(string = week_year, pattern = "^\\d+"),
        str_extract(string = week_year, pattern = "\\d*$"),
        sep = "/"
      ), "%w/%W/%Y"
    ), "%d/%m/%Y")
  )

# A tibble: 14 x 2
# week_year  day_month_year
#    <chr>       <chr>         
# 1 33/1988    15/08/1988    
# 2 37/1991    16/09/1991    
# 3 37/1992    14/09/1992    
# 4 41/1994    10/10/1994    
# 5 15/1997    14/04/1997    
# 6 18/2001    30/04/2001    
# 7 50/2001    10/12/2001    
# 8 16/2002    22/04/2002    
# 9 35/2004    30/08/2004    
# 10 5/2012    30/01/2012    
# 11 50/2012   10/12/2012    
# 12 8/2013    25/02/2013    
# 13 36/2013   09/09/2013    
# 14 51/2017   18/12/2017 

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

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