【发布时间】:2021-08-09 00:34:43
【问题描述】:
我想创建一个函数,它采用任意 data.table dt、查找表 dtLookup 并根据查找表有效地替换(即使用 data.table 内存框架)列 col 中的所有值.
这是原始代码:
dt <- data.table( chapter=as.character(11:15) );dt
dtLookup <- data.table(
old = c("11", "12", "14", "15"),
new = c("101", "102", "105", "104")
)
这可行(来自上面帖子的原始代码):
dt[
dtLookup,
on = c(chapter = "old"),
chapter := new
][]
但这不起作用:
dt.replaceValueUsingLookup <- function(dt, col, dtLookup) {
dt[
dtLookup,
on = c(as.name(col) = "old"),
as.name(col) := new
]
}
dt %>% dt.replaceValueUsingLookup("chapter", dtLookup)
我也试过这个:
dt[
dtLookup,
on = c(get(col) = "old"),
get(col) := new
]
它也不起作用。
【问题讨论】:
标签: r data.table