修改
我有两个不同长度的数据帧:
kframe <- data.frame(One=c("NO123","NO234", "NO345", "NO432"),Two=c("NAME1","NAME2","NAME3","NAME4"), Three=c(1,2,3,4))
lframe <- data.frame(One=c("NO123","NO587", "NO345"),Two=c("N1","N2","N2"), Three=c(2,3,4))
想要检查lframe$One[x]
中是否存在x=1:nrow(lframe)
(kframe$One
)中的值,如果为lframe$Two[x]
,则替换 One Two Three
1 NO123 NAME1 3
2 NO587 N2 3
3 NO345 NAME3 7
中的值,否则不执行任何操作。< / p>
怎么做?
编辑:
或者,换句话说:如果为true,请在$ Three中添加值...
预期结果:
LFRAME:
{{1}}
答案 0 :(得分:0)
你可以做一个简单的for循环:
for (i in 1:nrow(kframe)) {
if (lframe$One[i] == kframe$One[i]) {
lframe$Two[i] <- kframe$Two[i]
}
}