Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R-finding unmatched column names of data frames

Tags:

r

I have two large data frames(df1 and df2). I want to combine them using rbind function:

df<-rbind(df1,df2)

However, I get an error:

Error in match.names(clabs, names(xi)) : 
names do not match previous names

There more than 100 variables in the data frames. I know most names match. One or two names may not match. How can I find unmatched column names of df1 and df2. I will be very glad for any help. Thanks a lot.

like image 862
oercim Avatar asked Feb 28 '15 14:02

oercim


People also ask

How do you find unmatched records in R?

How to Find Unmatched Records in R?, To retrieve all rows in one data frame that do not have matching values in another data frame, use R's anti_join() function from the dplyr package.

How do I compare columns in different Dataframes in R?

We can compare two columns in R by using ifelse(). This statement is used to check the condition given and return the data accordingly. where, df is the input dataframe.

Does Rbind use column names?

For cbind ( rbind ) the column (row) names are taken from the colnames ( rownames ) of the arguments if these are matrix-like. Otherwise from the names of the arguments or where those are not supplied and deparse.

How do I get column names in Rstudio?

To access a specific column in a dataframe by name, you use the $ operator in the form df$name where df is the name of the dataframe, and name is the name of the column you are interested in. This operation will then return the column you want as a vector.


1 Answers

You could use setdiff:

a <- c("a", "b", "c")
b <- c("b", "c", "d")
setdiff(a, b)
#[1] "a"
setdiff(b, a)
#[1] "d"

where a is, e.g., names(df1).

like image 189
Roland Avatar answered Oct 16 '22 08:10

Roland