复杂的标题,但这是我想要实现的一个简单示例:
d <- data.frame(v1 = c(1,2,3,4,5,6,7,8),
v2 = c("A","E","C","B","B","C","A","E"))
m <- data.frame(v3 = c("D","E","A","C","D","B"),
v4 = c("d","e","a","c","d","b"))
通过匹配in 中的值,d$v2
应将 in 中的值替换为 in 中的值m$v4
d$v2
m$v3
生成的数据框d
应如下所示:
v1 v4
1 a
2 e
3 c
4 b
5 b
6 c
7 a
8 e
我尝试了不同的东西,最接近的是:d$v2 <- m$v4[which(m$v3 %in% d$v2)]
我尝试再次避免任何 for 循环!必须是可能的:-) 不知何故... ;)