简体   繁体   English

R中的日期转换格式

[英]Date conversion format in R

Hi i need to pass from character to date format My date in chr format is like: "2020-W14" so i need to pass it to date format嗨,我需要从字符格式传递到日期格式 我的 chr 格式日期类似于:“2020-W14”,所以我需要将它传递到日期格式

I m trying this with lubridate package;:我正在用 lubridate package 尝试这个;:

year_week="2020-W14"

date=as_date(year_week,format="%Y-W%W")

I get this date from this chunk of code but it is incorrect我从这段代码中得到这个日期,但它不正确

2020-01-16 2020-01-16

Any idea?任何的想法? thanks谢谢

You could try using the ISOweek package:您可以尝试使用 ISOweek package:


library(ISOweek)


year_week="2020-W14"


ISOweek2date(paste0(year_week, "-1")) #add a reference day

#> [1] "2020-03-30"

See Transform year/week to date object for a detailed explanation.详见Transform year/week to date object

Created on 2021-01-16 by the reprex package (v0.3.0)reprex package (v0.3.0) 创建于 2021-01-16

lubridate stores dates in days, so expects to also get a weekday. lubridate以天为单位存储日期,因此预计也会得到一个工作日。

library(lubridate)
year_week <- "2020-W14-0" # Sunday of 14th week

date <- as_date(year_week,format="%Y-W%W-%w")

## "2020-04-05"

library(ISOweek)图书馆(ISOweek)

year_week="2020-W14" year_week="2020-W14"

ISOweek2date(paste0(year_week, "-1")) #add a reference day ISOweek2date(paste0(year_week, "-1")) #添加参考日期

#> [1] "2020-03-30" #> [1] "2020-03-30"

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

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