Jingtong Feng
Jingtong Feng

Reputation: 3

How to rbind different data frames with different column names?

This is the first dataframe E I have

My goal is stack these similiar data frames as one, but I don't care the different column names they have.

First, my code is:

D <- rbind(E, N1, N2)

But it has the error:

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

Then I realized this happended because of the column names of each dataframe re not matching. So I tried the codes:

names(xd.small[[1]]) <- names(xd.small[[2]]) 
identical(names(xd.small[[1]]), names(xd.small[[2]]) )
[1] TRUE

After that, it still had the same error:

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

I just want to stack these three data frames together, and I don't care they don't share same column names. Is it possible?

Upvotes: 0

Views: 6782

Answers (1)

Cris
Cris

Reputation: 796

Simply use:

colnames(E)=colnames(N1)=colnames(N2)

D <- rbind(E, N1, N2)

Remember that for the rbind to work the dataframes should have the same number of columns.

Upvotes: 4

Related Questions