Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下两个数据框:
d <- data.frame(c1 = c("A","A","B","C","A","C","D","D")) map <- data.frame(c1 = c("A","B","C","D"), c2 = c(12,14,16,25))
如何将另一个名为“匹配”的列添加到包含数据框映射中找到的相应值的数据框 d?所以数据框 d 应该如下所示:
A 12 A 12 B 14 C 16 A 12 C 16 D 25 D 25
提前谢谢了!
使用调用的函数match:
match
d$match <- map$c2[match(d$c1,map$c1)]
由于这些级别的指定方式,您还可以执行以下操作:
d$match <- map$c2[d$c1]
但这只有在每一行都按顺序完全匹配因子match的水平时才有效。c1
c1