【发布时间】:2021-04-28 21:08:31
【问题描述】:
我有两个数据框(例如:df1、df2)。我想填写从 df1 到 df2 的一列(测试),条件是两个数据帧之间具有相同的 ID 和名称。我曾尝试使用 for 循环,但发现很难实现。结果应该是这样的形式:“输出”。我会很感激你的帮助。谢谢!
Code:
var_of_interest <- c("testing")
df2[var_of_interest] <- lapply(var_of_interest, function(x), df1[[x]][match(df2$ID, df1$ID) & match(df2$Name, df1$Name)])
df1: df1 is a subset of df2(It has information on testing)
ID Name testing
1 a 100
2 a 90
3 a 80
4 a 70
5 a 60
df2:
ID Name testing
1 a NA
2 a NA
2 b 400
3 a NA
3 c 300
4 a NA
4 d 200
5 a NA
5 e 150
output:
ID Name testing
1 a 100
2 a 90
2 b 400
3 a 80
3 c 300
4 a 70
4 d 200
5 a 60
5 e 150
【问题讨论】: