【发布时间】:2021-05-20 02:02:36
【问题描述】:
我需要合并两个数据集,其中相同的观察结果名称不同。为此,在合并这些集合之前,我需要根据查找日期对其中一个进行观察。用 data.table 的说法我该怎么做?
下面的例子,显然行不通:
# dataset:
dt <- data.table( chapter=as.character(11:15) );dt
# lookup table:
dtLookup <- data.table(
old = c("11", "12", "14", "15"),
new = c("101", "102", "105", "104")
)
# updated data.table with values replaced according to lookup table:
# I can do it this way, but it's obviously very slow not taking the data.table advantage
for(i in 1:nrow(dtLookup) ) {
dt[chapter == dtLookup$old[i], chapter:= dtLookup$new[i]]
};
【问题讨论】:
-
您希望输出什么?
标签: r data.table