Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3 dispatching of `rbind` and `cbind`

Tags:

r

I am trying to write an rbind method for a particular class. Here's a simple example where it doesn't work (at least for me):

rbind.character <- function(...) {
    do.call("paste", list(...))
}

After entering this function, I seemingly can confirm that it is a valid method that R knows about:

> methods("rbind")
[1] rbind.character  rbind.data.frame rbind.rootogram* rbind.zoo*      
see '?methods' for accessing help and source code

However, it is not recognized if I try to use it:

> rbind("abc", "xyz")
     [,1] 
[1,] "abc"
[2,] "xyz"
> #### compared with ####
> rbind.character("abc", "xyz")
[1] "abc xyz"

The help page says that dispatch is performed internally as follows:

  1. For each argument we get the list of possible class memberships from the class attribute.
  2. We inspect each class in turn to see if there is an applicable method.
  3. If we find an applicable method we make sure that it is identical to any method determined for prior arguments. If it is identical, we proceed, otherwise we immediately drop through to the default code.

With rbind("abc", "xyz"), I believe all these criteria are satisfied. What gives, and how can I fix it?

like image 555
Russ Lenth Avatar asked Dec 01 '15 15:12

Russ Lenth


1 Answers

attributes("abc")
#NULL

A character vector doesn't have a class attribute. I don't think a method can be dispatched by rbind for the implicit classes.

like image 134
Roland Avatar answered Oct 12 '22 22:10

Roland