且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

合并不同大小的数据框

更新时间:2023-12-05 17:03:22

根据您的描述,我知道您想替换 z d1 中的值与 d2 z 中的值 x & y 匹配。

From your description I understand that you want to replace the z values in d1 with the z values in d2 when x & y match.

使用基数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






使用数据。表 -package:


Using the 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)

由于版本0.5.0 也可以使用 coalesce -功能(为此向Laurent Hostert致谢,请将其带到我的计算机上注意):

Since release 0.5.0 you can also use the coalesce-function for this (thx to Laurent Hostert for bringing it to my attention):

d3 <- left_join(d1, d2, by = c("x","y")) %>% 
  mutate(z = coalesce(z.y, z.x)) %>% 
  select(-c(z.x, z.y))