We can use Reduce
and intersect
Reduce(intersect, lapply(mget(ls(pattern = "df\\d+")), names))
#[1] "a" "c"
pattern
argument in ls()
should include the pattern your dataframes follow. In this example, I have considered the dataframes to be df1
, df2
and df3
. So it follows a pattern "df"
followed by a number which I have mentioned in the pattern
argument (df\\d+
). You need to change this based on the pattern your dataframes have or you can also include them manually in a list if they do not have any pattern.
Reduce(intersect, lapply(list(df1, df2, df3), names))
#[1] "a" "c"
If you want to subset the common columns
list_df <- mget(ls(pattern = "df\\d+"))
common_cols <- Reduce(intersect, lapply(list_df, names))
lapply(list_df, `[`, common_cols)
#$df1
# a c
#1 1 11
#2 2 12
#3 3 13
#4 4 14
#5 5 15
#$df2
# a c
#1 1 11
#2 2 12
#3 3 13
#4 4 14
#5 5 15
#$df3
# a c
#1 1 11
#2 2 12
#3 3 13
#4 4 14
#5 5 15
data
df1 <- data.frame(a = 1:5, b = 2:6, c = 11:15)
df2 <- data.frame(a = 1:5, c = 11:15)
df3 <- data.frame(a = 1:5, b = 2:6, c = 11:15, d = 21:25)