0

I tried to find the the documentation for the function rbind.data.frame, to understand what the following finction call does (found in https://stackoverflow.com/a/4227483/3999893):

rbind.data.frame(list(a=1, b=2), list(a=3, b=5), list(a=5, b=15))

But using ?rbind.data.frame only gave an opaque help for rbind.

Is rbind.data.frame some kind of method-chanining, or are there simply no docs for that method?

Community
  • 1
  • 1
Edward
  • 4,453
  • 8
  • 44
  • 82
  • 1
    It's in `?rownames`. See: http://stackoverflow.com/questions/5524140/rbind-in-r-gives-a-weird-rowname – Alexey Ferapontov Jul 23 '15 at 14:54
  • 1
    Believe it or not, this is a duplicate of [this SO post](http://stackoverflow.com/questions/5524140/rbind-in-r-gives-a-weird-rowname). Basically, your lists are generating hypothetical row names of `2, 2, and 3`. R requires that all row names be unique so it changes the second one to `21`. – Tim Biegeleisen Jul 23 '15 at 14:56
  • 4
    You're calling `rbind.data.frame` on *not a data.frame*... explicitly calling a method on an object it wouldn't dispatch to is not good practice. It's especially bad when you call a method on an object from which the class of the method inherits from. – Joshua Ulrich Jul 23 '15 at 14:56
  • @JoshuaUlrich : What's the alternative? I'd like to build a data.frame from a list of lists. And the rbind.data.frame is what I found: http://stackoverflow.com/a/4227483/3999893 – Edward Jul 23 '15 at 15:08
  • 2
    Convert your list of lists to a list of data.frames. `L <- list(list(a=1, b=2), list(a=3, b=5), list(a=5, b=15)); do.call(rbind, lapply(L, data.frame))`. – Joshua Ulrich Jul 23 '15 at 15:15
  • @JoshuaUlrich : So when to use `rbind.data.frame`? and where is the documenation for that function? Or is the `## S3 method for class 'data.frame'` in `?rbind` the "documenation"? – Edward Jul 23 '15 at 15:25
  • I think the members of R-Core would say the ?rbind-page is part of the documentation and that if you need more specifics you should read the source code. You can read the uncommented source code by typing `rbind.data.frame`. – IRTFM Jul 23 '15 at 16:19
  • @BondedDust could you write an answer? because you clearly understand what is going on much better than me :) I totally get what rbind.data.frame is doing for data frames because I can read the source code for that. What is rbind doing for not dataframes? Why would it do that? Maybe I should write a question... – Mhairi McNeill Jul 23 '15 at 16:32

2 Answers2

4

rbind is generic, but it has no rbind.default method that appears when you type:

 methods(rbind)

Instead, when you type rbind at the console you will see:

function (..., deparse.level = 1) 
.Internal(rbind(deparse.level, ...))
<bytecode: 0x100e55f20>
<environment: namespace:base>

So it is not correct that rbind is only implemented for dataframes in pkg:base. Instead there will be a call to .Internal(rbind(deparse.level, ...). The logic of the .Internal(rbind(..)) function then supplants the usual S3 logic so it can check whether any of the items in the argument list are data.frames. If not then it continues on to process atomic vectors and matrices. For example on my machine at the moment I see 4 methods listed:

methods(rbind)
[1] rbind.data.frame rbind.matrix.csr rbind.Predict*  
[4] rbind.zoo*  

It is also imprecise to say rbind.data.frame only applies to dataframes. More precise would be to use the language in the second sentence of Details section of ?rbind. And to actually read the rest of the help page which Joshua has now reminded us all to read.

The lack of an rbind.default is a departure from the usual method of handling S3 generic functions. Most S3-generics that I have examined have had *.default methods. Some, like plot.default even have their own help pages.

And finally, I have no clue why the rownames appear the way they do in the original question.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 2
    Both `rbind` and `cbind` have different method dispatch semantics than most generic functions. This is so they can check the class(es) of all the arguments before dispatching. See the *Dispatch* section of `?rbind`. – Joshua Ulrich Jul 23 '15 at 16:41
  • That makes complete sense. I should have scrolled further down the help page (as always). – IRTFM Jul 23 '15 at 16:55
1

rbind is a generic function, rbind.data.frame is the version of this function that applies to data frames.

If you do rbind on a data frame you will be doing rbind.data.frame.

Presumably different objects could have different rbind functions applied to them.

More information:

http://www.burns-stat.com/pages/Tutor/R_inferno.pdf - Look at the start of chapter 7

I think the reason you only see help for rbind is that rbind is currently only implemented for data frames in base R so rbind == rbind.data.frame.

Mhairi McNeill
  • 1,951
  • 11
  • 20