简体   繁体   English

合并多个数据帧的列,根据列名覆盖值

[英]Merge columns of multiple data frames overwriting values based on column names

I have the following data: 我有以下数据:

x<-data.frame(A=c(1,NA),B=c(NA,NA),C=c(NA,1))
   A  B  C
1  1 NA NA
2  NA NA  1

and

y<-data.frame(A=c(NA,0),C=c(NA,NA),D=c(NA,0))
   A  C  D
1 NA NA NA
2  0 NA  0

and I want to merge them in such a way, that columns of the same name are overwritten giving precedence to non - NA values. 我希望以这种方式合并它们,同名的列被覆盖,优先于非NA值。 In addition, columns that are not common should be added. 此外,应添加不常见的列。

Desired result: 期望的结果:

  A  B  C  D
1 1 NA NA NA
2 0 NA  1  0

There are no conflicts between non - NA values in my data. 我的数据中的非NA值之间没有冲突。

you can try: 你可以试试:

library(dplyr)
library(magrittr)

full_join(x,y) %>%
      inner_join(y) %>%
      coalesce(full_join(x,y) %>%
                 inner_join(x))

which gives: 这使:

  A  B  C  D
1 1 NA NA NA
2 0 NA  1  0

Here is a base R method that should be fairly general. 这是一个基本的R方法,应该相当通用。

# fill in x NA values with values in y
x[] <- lapply(names(x), function(i) ifelse(is.na(x[,i]) & i %in% names(y), y[,i], x[,i]))

# add variables in y that are not in x and provide names to the variables
setNames(cbind(x, y[, !(names(y) %in% names(x))]), union(names(x), names(y)))
  A  B  C  D
1 1 NA NA NA
2 0 NA  1  0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM