【发布时间】:2020-05-10 07:09:04
【问题描述】:
我的数据具有字符格式的日期,例如“JAN2005”、“FEB2005”、“MAR2005”,例如:
Test <- data.table(c("JAN2005","FEB2005","MAR2005","APR2005"),c(436.6,543.1,417.3,687.4))
有没有一种简单的方法来获取实际日期格式的日期?
【问题讨论】:
-
试试
Test[, V1 := as.Date(zoo::as.yearmon(V1))] -
或
base RTest[, V1 := as.Date(paste0(V1, '01'), "%b%Y%d")]# > Test$V1 [1] "2005-01-01" "2005-02-01" "2005-03-01" "2005-04-01" -
可能与语言环境有关,即如果您的语言环境设置不同,则可能与月份不匹配。根据
?strptime%b -Abbreviated month name in the current locale on this platform. (Also matches full name on input: in some locales there are no abbreviations of names.) -
我的区域信息是
locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 -
请查看here 设置区域设置
标签: r date datatable data-cleaning