我有一个数据框,其中包含多个日期值作为字符串的列(格式:YYYYMMDD)。
dt_example <- data.frame (
W03 = "20201130",
W44 = "19711031",
P01 = "19740813",
P04 = "20000506",
Z02 = "20201231"
)
现在我想将字符串转换为日期格式并尝试了以下代码,它工作正常:
dt_example_date <- dt_example %>%
mutate(
W03 = as.Date(W03, "%Y%m%d")
,W44 = as.Date(W44, "%Y%m%d")
,P01 = as.Date(P01, "%Y%m%d")
,P04 = as.Date(P04, "%Y%m%d")
,Z02 = as.Date(Z02, "%Y%m%d")
)
但是这段代码很烦人,我想使用“mutate across”而不是多重 as.Date-conversions。
我尝试了几种替代方法,但没有任何效果。
dt_example_date_2 <- dt_example %>%
mutate(
across(
c("W03","W44","P01","P04","Z02")
,as.Date
)
)
结果:字符串不是标准的明确格式
dt_example_date_2 <- dt_example %>%
mutate(
across(
c("W03","W44","P01","P04","Z02")
,as.Date(format="%y%m%d")
)
)
结果:缺少参数“x”
dt_example_date_2 <- dt_example %>%
mutate(
across(
c("W03","W44","P01","P04","Z02")
,as.Date(., format="%y%m%d")
)
)
结果:不知道如何转换'.' 到“日期”类</p>
我没有得到它,不知道该怎么办。
如何将参数传递给跨命令中使用的函数?
问候本尼