Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R rbind dataframes returns a list [closed]

I have run into a problem and managed to solve it with a hack and I wish to understand the problem and hopefully get rid of the hack.

I tried recreating the problem to no avail, so words will have to suffice here.

I am trying to rbind two dataframes in R, the result of which must again be a dataframe, not a list. I use rbind in most of my scripts and have never had an issue before.

However, today I applied rbind to two dataframes, say foo and bar and it returned a list foobar. The hack I use to fix this is to force-convert foo and bar to dataframes again as follows:

rbind(data.frame(foo), data.frame(bar))

This works, but I would like to know why I have to convert it explicitly when both foo and bar are already data.frames.

My question is then in what 'general' scenarios would rbind return a list when both inputs are data.frames?

I tried debugging it by looking at rbind(A,A) and rbind(B,B). Both times it returns a dataframe and not a list. Why then would rbind(A,B) return a list?

Thanks!

like image 406
gmarais Avatar asked Jan 11 '18 15:01

gmarais


People also ask

What does Rbind return in R?

Example 3: rbind fill – Row Bind with Missing Columns The binding of data frames with different columns / column names is a bit more complicated with the rbind function. R usually returns the error “Error in match. names(clabs, names(xi))”, if you try to use the rbind function for data frames with different columns.

How do I combine a list of Dataframes in R?

To combine data frames stored in a list in R, we can use full_join function of dplyr package inside Reduce function.

What is the difference between Rbind and Bind_rows?

rbind throws an error in such a case whereas bind_rows assigns " NA " to those rows of columns missing in one of the data frames where the value is not provided by the data frames.

How does Rbind work in R?

Combine Vectors, Matrix or Data Frames by Rows in R Language – rbind() Function. rbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by rows. deparse. level: This value determines how the column names generated.


1 Answers

If one of your data frames is in fact a tibble, you'll need to use dplyr::bind_rows() instead of rbind(), as dplyr::bind_rows() is specifically designed to deal with tibbles (it also works with data frames in general). Since tibbles are an invention of the tidyverse, it is not necessarily fully compatible with base functions like rbind() (I myself was unaware that such a behaviour would occur until you brought it up).

More information about the difference between the two functions, and why you may want to use dplyr::bind_rows() over rbind(), can be found here.

like image 65
Phil Avatar answered Oct 12 '22 13:10

Phil