我有两个数据框d1
,d2
分别为:
x y z 10 10 7 10 12 6 11 10 8 11 12 2 12 10 1 12 12 5 x y z 10 10 100 11 10 200 12 12 400
我想要的东西:
x y z 10 10 100 10 12 6 11 10 200 11 12 2 12 10 1 12 12 400
我真的很抱歉这个微不足道的问题,我无法得到答案.
根据您的描述,我了解您要使用when 和match中的值替换z
值.d1
z
d2
x
y
使用基数R:
d3 <- merge(d1, d2, by = c("x","y"), all.x = TRUE) d3[is.na(d3$z.y),"z.y"] <- d3[is.na(d3$z.y),"z.x"] d3 <- d3[,-3] names(d3)[3] <- "z"
这使:
> d3 x y z 1 10 10 100 2 10 12 6 3 11 10 200 4 11 12 2 5 12 10 1 6 12 12 400
使用data.table -package:
library(data.table) setDT(d1) # convert the data.frame to a data.table setDT(d2) # idem # join the two data.table's and replace the values d1[d2, on = .(x, y), z := i.z]
或者一气呵成:
setDT(d1)[setDT(d2), on = .(x, y), z := i.z]
这使:
> d1 x y z 1: 10 10 100 2: 10 12 6 3: 11 10 200 4: 11 12 2 5: 12 10 1 6: 12 12 400
使用dplyr包:
d3 <- left_join(d1, d2, by = c("x","y")) %>% mutate(z.y = ifelse(is.na(z.y), z.x, z.y)) %>% select(-z.x) %>% rename(z = z.y)