16

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!

gmarais
  • 1,801
  • 4
  • 16
  • 32
  • 3
    Hard to say without having access to `foo` and `bar` – bouncyball Jan 11 '18 at 15:52
  • 1
    It works for me. Make sure you have same variable name. – patL Jan 11 '18 at 15:54
  • @patL How do you know it works for you when you haven't seen the OP's actual data? – Tim Biegeleisen Jan 11 '18 at 15:54
  • 1
    I am thinking *foo* and *bar* are not actually dataframes, possibly dataframes within a list or list of vectors. Please `dput` these objects for us. – Parfait Jan 11 '18 at 15:54
  • @Parfait, as per Occam's razor, you are right. I ASSumed `bar` was a data.frame but in fact it is a `tibble`...which is a whole new thing for me! Do I close the question?? Thanks for the help – gmarais Jan 11 '18 at 16:00
  • 1
    Since this will not help future readers but simple overlook of your environment, deleting this may be warranted. – Parfait Jan 11 '18 at 16:04
  • @gmarais if it's a tibble, try `dplyr::bind_rows()` instead of `rbind()`? That may resolve your specific issue. – Phil Jan 11 '18 at 16:32
  • @Phil that worked thanks! Unfortunately I asked the wrong question... I think you can still post the answer and explain that your solution works when `foo` or `bar` aren't guaranteed data.frames. I will gladly accept it as it does 'solve' my problem. – gmarais Jan 11 '18 at 16:41

1 Answers1

22

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.

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    Thanks for this answer. Although I agree the question wasn't the best, I have been struggling with exactly the same point: why a simple rbind yielded only a list and not a data.table. – W Barker Mar 19 '21 at 18:52