1

I have 6 data sets. Their names are: e10_all, e11_all, e12_all, e13_all, e14_all, and e19_all.

All have different numbers of columns and rows, but with some common columns. I need to bind the rows of these columns together. First, I want to determine the columns that are common to all of the data sets. Each of these data sets has around 100 columns, so checking them one-by-one would be very tedious. I've tried a few different methods of comparing two data sets, but with no luck.

I've tried:

library(arsenal)
summary(compare(e10_all, e11_all))


library(lubridate)
setdiff(e10_all, e11_all)


c <- cbind(e10_all[, which(colnames(e10_all)%in% colnames(e11_all))],
           e11_all[, which(colnames(e11_all)%in% colnames(e10_all))])


as.data.frame(lapply(intersect(names(e10_all), names(e11_all)), 
                     function(name) e10_all[name] + e11_all[name]))

None of these give me what I want. All I need is a list of columns that are common between the two data sets. If possible I would like to compare all 6 data sets.

Thank you

1
  • 3
    perhaps this will help? common_cols <- intersect(colnames(df1), colnames(df2)) Commented Jul 11, 2018 at 14:41

3 Answers 3

4

Addition to Wimpel's answer you can search by the names()

intersect(names(dataframe1), names(dataframe2))
Sign up to request clarification or add additional context in comments.

Comments

2

What I would do is create a list with all the data frame, taking the column names of the first data frame and then loop on the other data frame to take only the column that are common to each of them

list_df = list(e10_all,e11_all,e12_all,e13_all,e14_all,e19_all)

col_common = colnames(list_df[[1]])
for (i in 2:length(list_df)){
  col_common = intersect(col_common, colnames(list_df[[i]]))
}

Comments

2

create vectors with columnnames, and then use intersect()

v1 <- c("test", "test2", "test4")
v2 <- c("test", "test1", "test2")

intersect(v1, v2)

#[1] "test"  "test2"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.