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.
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)
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
Or in base R
nm1 <- Reduce(intersect, lapply(lst1, names))
out <- do.call(rbind, lapply(lst1, subset, select = nm1))
row.names(out) <- NULL
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.