[英]R data.table: efficient ways to add days (01) to a date
I have the following data.table
:我有以下data.table
:
library(data.table)
DT <- data.table(n = c("A", "B"), dt_start = c("10/2020", "05/2015"))
I want:我想:
DT <- data.table(n = c("A", "B"), dt_start = c("2020-10-01", "2015-05-01"))
You can use paste0
to add the day to the character and use as.Date
to coerce it:您可以使用paste0
将日期添加到字符并使用as.Date
强制它:
DT[, dt_start := as.Date(paste0("01/", DT$dt_start), "%d/%m/%Y")]
The result is结果是
DT
n dt_start
1: A 2020-10-01
2: B 2015-05-01
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.