你可以用
mapply
.你需要改变一下你的策略。通过
names
属于
claims
发挥作用
映射
然后返回转换后的数据
声称
数据帧。
as.data.frame(mapply(function(fun.col){
col.type <- claims.mapping$x[match(fun.col,
claims.mapping$col.name.std)]
if (is.na(col.type)) {
claims[,fun.col]
} else if (col.type == "Date") {
as.Date(claims[,fun.col], origin = "1990-01-01")
} else{
claims[,fun.col]
}
},names(claims), SIMPLIFY = FALSE))
# a b
# 1 2026-04-07 1
# 2 2108-05-02 2
# 3 2113-10-24 3
从OP修正的数据:
claims <- structure(list(a = c(13245, 43220, 45221), b = 1:3), .Names = c("a", "b"), row.names = c(NA, -3L), class = "data.frame")
structure(list(a = 10:20, b = 21:31), .Names = c("a", "b"), row.names = c(NA, -11L), class = "data.frame")
claims.mapping <- structure(list(col.name.std = structure(1:3, .Label = c("a", "b", "c"), class = "factor"), x = c("Date", "numeric", "character")), row.names = c(NA, -3L), class = "data.frame")