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!
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.