简体   繁体   English

将日期转换为年份和周数并返回

[英]Converting date to year and week number and back

I want to convert a date to a combination of year and week number.我想将日期转换为年份和周数的组合。 Later I want to convert back to date again.后来我想再次转换回日期。 I do not understand why it is not possible to arrive back at the same original date, even when I try all the different start of the week numbers:我不明白为什么不能在相同的原始日期返回,即使我尝试了所有不同的开始日期:

library(dplyr)

df <- tidyr::expand_grid(
  date = as.Date(paste("2021 12 29"), "%Y %m %d") + 0:10,
  weekday = 0:6
)

df <- df %>%
  mutate(
    year = format(date, "%Y"),
    week = format(date, "%W"),
    newDate = as.Date(paste(year, week, weekday), format = "%Y %W %w")
  ) %>%
  tidyr::pivot_wider(
    id_cols = c("date"), names_from = "weekday", 
    values_from = "newDate"
  )

print(df)
# A tibble: 11 x 8
   date       `0`        `1`        `2`        `3`        `4`        `5`        `6`       
   <date>     <date>     <date>     <date>     <date>     <date>     <date>     <date>    
 1 2021-12-29 2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31 NA        
 2 2021-12-30 2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31 NA        
 3 2021-12-31 2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31 NA        
 4 2022-01-01 NA         NA         NA         NA         NA         NA         2022-01-01
 5 2022-01-02 NA         NA         NA         NA         NA         NA         2022-01-01
 6 2022-01-03 2022-01-02 2022-01-03 2022-01-04 2022-01-05 2022-01-06 2022-01-07 2022-01-08
 7 2022-01-04 2022-01-02 2022-01-03 2022-01-04 2022-01-05 2022-01-06 2022-01-07 2022-01-08
 8 2022-01-05 2022-01-02 2022-01-03 2022-01-04 2022-01-05 2022-01-06 2022-01-07 2022-01-08
 9 2022-01-06 2022-01-02 2022-01-03 2022-01-04 2022-01-05 2022-01-06 2022-01-07 2022-01-08
10 2022-01-07 2022-01-02 2022-01-03 2022-01-04 2022-01-05 2022-01-06 2022-01-07 2022-01-08
11 2022-01-08 2022-01-02 2022-01-03 2022-01-04 2022-01-05 2022-01-06 2022-01-07 2022-01-08

Why is there not a column that does not contains NAs?为什么没有不包含 NA 的列?

Output of sessionInfo() : sessionInfo()输出:

R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

Right now you are setting newDate to be equal to a specific year a specific month and then a range over a range of 7 days.现在,您将 newDate 设置为等于特定月份的特定年份,然后是 7 天范围内的范围。 Which is why newDate is currently:这就是为什么 newDate 目前是:

"2021-01-03" "2021-01-04" "2021-01-05" "2021-01-06" "2021-01-07" "2021-01-01" "2021-01-02" NA

To get your desired output of returning the same date you initially had, you have to store the day as well and reference it in the creation of newDate.要获得返回与最初相同日期的所需输出,您还必须存储该日期并在 newDate 的创建中引用它。 In short, do this:简而言之,这样做:

date <- as.Date(paste("2021 01 03"), "%Y %m %d")
year <- format(date, "%Y")
week <- format(date, "%W")
day <- format(date, "%d")
newDate <- as.Date(paste(year, week, day), format = "%Y %W %w")

%G and %V solved my problem: %G 和 %V 解决了我的问题:

library(dplyr)

df <- tidyr::expand_grid(
  date = as.Date(paste("2021 12 26"), "%Y %m %d") + 0:10,
  weekday = 0:6
)

df <- df %>%
  mutate(
    year = format(date, "%G"),
    week = format(date, "%V"),
    newDate = as.Date(paste(year, week, weekday), format = "%Y %W %w")
  ) %>%
  tidyr::pivot_wider(
    id_cols = c("date", "year", "week"), names_from = "weekday", 
    values_from = "newDate"
  )

print(df)
# A tibble: 11 x 10
   date       year  week  `0`        `1`        `2`        `3`        `4`        `5`       
   <date>     <chr> <chr> <date>     <date>     <date>     <date>     <date>     <date>    
 1 2021-12-26 2021  51    2021-12-19 2021-12-20 2021-12-21 2021-12-22 2021-12-23 2021-12-24
 2 2021-12-27 2021  52    2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31
 3 2021-12-28 2021  52    2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31
 4 2021-12-29 2021  52    2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31
 5 2021-12-30 2021  52    2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31
 6 2021-12-31 2021  52    2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31
 7 2022-01-01 2021  52    2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31
 8 2022-01-02 2021  52    2021-12-26 2021-12-27 2021-12-28 2021-12-29 2021-12-30 2021-12-31
 9 2022-01-03 2022  01    2022-01-02 2022-01-03 2022-01-04 2022-01-05 2022-01-06 2022-01-07
10 2022-01-04 2022  01    2022-01-02 2022-01-03 2022-01-04 2022-01-05 2022-01-06 2022-01-07
11 2022-01-05 2022  01    2022-01-02 2022-01-03 2022-01-04 2022-01-05 2022-01-06 2022-01-07
# ... with 1 more variable: 6 <date>

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

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