【发布时间】:2018-09-17 19:42:42
【问题描述】:
我有两个数据框 --- 表 A 是带有参考名称的模式表,表 B 是旧名称表。我想对表 B 进行子集化,它与表 a 中的模式匹配,当单元格匹配时,用 A 中的更新列更新 B 中的新列。
我引用了apply regexp in one data frame based on the column in another data frame,但它并不能解决这个问题。
A <- data.frame(pattern = c("ab", "be|eb", "cc", "dd"),
ref = c("first", "second", "third", "forth"))
B <- data.frame(name = c("aa1", "bb1", "cab", "ccaa" "abed" ,"ddd", "ebba"))
B$new = ""
我希望我的结果表是:
name new
cab first
abed second
ccaa third
ddd forth
ebba second
我在尝试
for (i in 1:nrow(B)) {
if (as.data.table(unlist(lapply(A$pattern, grepl, B$name))) == TRUE) {
B$new[i] = A$update
}
}
有人知道更好的解决方案吗?我更喜欢使用 apply 系列,但我不知道如何添加列。任何帮助表示赞赏!
【问题讨论】: