简体   繁体   中英

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

I m trying this with 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

Any idea? thanks

You could try using the 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.

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

lubridate stores dates in days, so expects to also get a weekday.

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)

year_week="2020-W14"

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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