1

What is a clean way to get around the following issue? I want to cbind a list of zoo objects with do.call.

>> zz <- list( zoo(1:10,1:10), zoo(101:110,1:10), zoo(201:210,1:10) )
>> names(zz)<-c('test','bar','foo')
>> do.call(cbind,zz)
>    test bar foo
> 1     1 101 201
> 2     2 102 202
> 3     3 103 203
> 4     4 104 204
> 5     5 105 205
> 6     6 106 206
> 7     7 107 207
> 8     8 108 208
> 9     9 109 209
> 10   10 110 210
>> names(zz)<-c('test','all','foo')
>> do.call(cbind,zz)
>    test foo
> 1     1 201
> 2     2 202
> 3     3 203
> 4     4 204
> 5     5 205
> 6     6 206
> 7     7 207
> 8     8 208
> 9     9 209
> 10   10 210

Because 'all' is the name of one of the arguments to cbind.zoo:

R> args(cbind.zoo)
function (..., all = TRUE, fill = NA, suffixes = NULL, drop = FALSE)
NULL

do.call constructs a call somewhat like:

R> cbind(test=zz$test, all=zz$all, foo=zz$foo)

The same thing would happen for list elements named 'fill', 'suffixes', or 'drop'.

  • `do.call(cbind.data.frame, zz)` would also work, no? Or even `as.data.frame(zz)`, as long as all `zz` elements are the same length. – Rich Scriven Oct 27 '14 at 23:00

1 Answers1

1

The best way is to avoid the reserved names but if you really must have them then try this:

setNames(do.call(cbind, unname(zz)), names(zz))
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341