r-将日期转换为年和周编号并返回



我想将日期转换为年和周数字的组合。稍后我想再次转换为最新版本。我不明白为什么不可能在同一个原始日期返回,即使我尝试了所有不同的周初数字:

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

为什么没有一列不包含NA?

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    

现在您正在将newDate设置为等于特定年份、特定月份,然后是7天的范围。这就是为什么newDate当前为:

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

为了获得返回最初日期的期望输出,您还必须存储该日期,并在创建newDate时引用它。简而言之,这样做:

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和%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>

相关内容

  • 没有找到相关文章

最新更新