Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rbind multiple data sets [duplicate]

Tags:

r

I have 3 data sets that I want to rbind together. I have renamed my columns to be the same:

names(DF1) <- c("A", "B", "C") names(DF2) <- c("A", "B", "C") names(DF3) <- c("A", "B", "C") 

They have each got different numbers of observations (34, 54, 23, respectively)

However, when I try with an rbind function, it returns the error:

total <- rbind(DF1, DF2, DF3) 

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

From other answered questions the error should arise because of differently named columns, but I have checked and rechecked that they have been renamed the same.

I would like to end up with a total dataset with a total of 111 observations with column titles. I am a beginner to R, so many of the answers from other questions elude me. Would anyone be able to answer this in layman terms?

like image 480
user2305783 Avatar asked Apr 22 '13 02:04

user2305783


People also ask

Can you use Rbind for multiple data frames?

Rbind as is only accepts two dataframes, so we have to adjust our code slightly to accommodate for more dataframes.

How do I append multiple data sets in R?

To join two data frames (datasets) vertically, use the rbind function. The two data frames must have the same variables, but they do not have to be in the same order. If data frameA has variables that data frameB does not, then either: Delete the extra variables in data frameA or.

How do I combine multiple data frames into one?

The concat() function can be used to concatenate two Dataframes by adding the rows of one to the other. The merge() function is equivalent to the SQL JOIN clause. 'left', 'right' and 'inner' joins are all possible.

Can you merge more than 2 datasets in R?

The merge function in R allows you to combine two data frames, much like the join function that is used in SQL to combine data tables. Merge , however, does not allow for more than two data frames to be joined at once, requiring several lines of code to join multiple data frames.


2 Answers

You can use do.call, like so:

do.call("rbind", list(DF1, DF2, DF3)) 

Note that second argument of do.call is a list.

like image 167
CHP Avatar answered Sep 21 '22 21:09

CHP


The tidyverse approach is to use bind_rows() from the dplyr package:

bind_rows(DF1, DF2, DF3) 
like image 30
Jot eN Avatar answered Sep 20 '22 21:09

Jot eN