0

In many R functions the ... argument is used to supply several objects. How can I supply a list object in a ... argument?

For example:

x1 <- head(iris)
x2 <- tail(iris)
rbind(x1, x2)
#    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#1            5.1         3.5          1.4         0.2    setosa
#2            4.9         3.0          1.4         0.2    setosa
#3            4.7         3.2          1.3         0.2    setosa
#4            4.6         3.1          1.5         0.2    setosa
#5            5.0         3.6          1.4         0.2    setosa
#6            5.4         3.9          1.7         0.4    setosa
#145          6.7         3.3          5.7         2.5 virginica
#146          6.7         3.0          5.2         2.3 virginica
#147          6.3         2.5          5.0         1.9 virginica
#148          6.5         3.0          5.2         2.0 virginica
#149          6.2         3.4          5.4         2.3 virginica
#150          5.9         3.0          5.1         1.8 virginica

But assume that I have the x1 and x2 data frames in a list. How can I use that list with the ... argument in rbind?

I've already tried the following, with no success:

rbind(list(x1, x2))
rbind(substitute(list(x1, x2))[-1])
rbind(unlist(list(x1, x2)))

In each of these cases the result is NOT as that from rbind(x1, x2). What am I missing?

UPDATE1:
As suggested in the answers, do.call(rbind, list(x1, x2)) seems to work perfectly in this case. However, I'm wondering if it's possible to avoid do.call machinery. In other words, is it possible to convert list(x1, x2) so that ... understands it correctly?

3
  • 1
    Almost a duplicate: stackoverflow.com/q/6192138/324364
    – joran
    Feb 13, 2014 at 18:14
  • do.call is a fabulous function, and I am sure you have hurt its feelings when you say you want to avoid it for no good reason. I also don't understand why you think rbind(list) would work. As you say, rbind takes several objects in .... a list is a single object.
    – rawr
    Feb 13, 2014 at 18:47
  • @rawr No offense was intended. :) As for list, it is a collection of several objects. I was hoping that a trick similar to unlist(list) would automagically pass the several embedded objects to rbind().
    – landroni
    Feb 13, 2014 at 21:15

2 Answers 2

3

do.call is what you use in this case.

df1 <- head(iris)
df2 <- tail(iris)
l <- list(df1, df2)

do.call(rbind, l)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 1            5.1         3.5          1.4         0.2    setosa
## 2            4.9         3.0          1.4         0.2    setosa
## 3            4.7         3.2          1.3         0.2    setosa
## 4            4.6         3.1          1.5         0.2    setosa
## 5            5.0         3.6          1.4         0.2    setosa
## 6            5.4         3.9          1.7         0.4    setosa
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
3
  • This works perfectly. But I was wondering if it's possible to avoid do.call machinery?
    – landroni
    Feb 13, 2014 at 18:18
  • 1
    @landroni Why do you want to avoid it? Feb 13, 2014 at 18:19
  • For no good, reason, I guess. I was wondering if this could be done.
    – landroni
    Feb 13, 2014 at 18:21
1

plyr has a function that turns other functions to a form that accepts lists as arguments. It's basically just a wrapper to do.call, but if this is something you do a lot you could define your own custom functions:

library(plyr)
rbindl <- splat(rbind)
lst <- list(head(iris, 3), tail(iris, 3))
rbindl(lst)
#     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
# 1            5.1         3.5          1.4         0.2    setosa
# 2            4.9         3.0          1.4         0.2    setosa
# 3            4.7         3.2          1.3         0.2    setosa
# 148          6.5         3.0          5.2         2.0 virginica
# 149          6.2         3.4          5.4         2.3 virginica
# 150          5.9         3.0          5.1         1.8 virginica

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.