简体   繁体   中英

R how get a date from variable in character

I have data that has dates in a character format, as "JAN2005","FEB2005","MAR2005", such as :

Test <- data.table(c("JAN2005","FEB2005","MAR2005","APR2005"),c(436.6,543.1,417.3,687.4))

Is there a simple way to get the date in an actual date format ?

We can use as.yearmon from zoo

library(data.table)
Test[, V1 := as.Date(zoo::as.yearmon(V1))]

Or convert to a proper 'Date' by paste ing the day as well and then use as.Date

Test[, V1 := as.Date(paste0(V1, '01'), "%b%Y%d")]

If the locale is different from English, change it to match the 'month' as it is in English

Sys.setlocale("LC_ALL","English")

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