本文介绍了将多个数据框与重合列合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将多个DataFrame和一些重合的列合并到一个新的DataFrame中。新数据框的列应该是重合列。
I would like to combine multiple DataFrames with some coincident columns, into a new DataFrame. The columns of the new DataFrame should be the coincident columns.
例如,假设我有数据框df1,df2,df3:
For example, suppose I have dataframes df1, df2, df3:
df1:
A B C D
1 2 3 4
df2:
A C D E
1 2 -1 5
df3:
C D F G
0 -1 0 7
New dataframe
C D
3 4
2 -1
0 -1
我尝试在循环中使用匹配功能找到重合的列的方式:
I have tried using match function in a circular way, to find the coincident columns:
match(df1,df2)
match(df2,df3)
match(df3,df1)
时间和线路,如果我有很多DataFrame。有人可以建议一个更好的方法吗?
It takes a lot of time and lines, if I have many DataFrames. Could anyone suggest a better way to do that?
推荐答案
一种选择是将数据集保存在中列表
并找到相交
列名
An option is to get the datasets in a list
and find the intersect
ing column name
library(tidyverse)
lst1 <- mget(paste0("df", 1:3))
nm1 <- map(lst1, names) %>%
reduce(intersect)
map_dfr(lst1, ~ .x %>%
select(nm1))
# C D
#1 3 4
#2 2 -1
#3 0 -1
或在 base R
nm1 <- Reduce(intersect, lapply(lst1, names))
out <- do.call(rbind, lapply(lst1, subset, select = nm1))
row.names(out) <- NULL
数据
data
df1 <- structure(list(A = 1L, B = 2L, C = 3L, D = 4L), class = "data.frame", row.names = c(NA,
-1L))
df2 <- structure(list(A = 1L, C = 2L, D = -1L, E = 5L), class = "data.frame", row.names = c(NA,
-1L))
df3 <- structure(list(C = 0L, D = -1L, F = 0L, G = 7L), class = "data.frame", row.names = c(NA,
-1L))
这篇关于将多个数据框与重合列合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!