[R] cbind question, please

William Dunlap wdunlap at tibco.com
Fri Apr 24 01:14:30 CEST 2015


You could do something tricky like
  > do.call(cbind, lapply(big.char, as.name))
       dog cat tree
  [1,]   1   2    5
  [2,]   2   3    6
  [3,]   3   4    7
but you are usually better off creating these things as part of a list
and passing that to do.call(cbind, list).

There is a slight danger of using do.call with cbind.  If your
list has a component with the unlikely name 'deparse.level',
then that will be taken as cbind's deparse.level argument,
not as a column of the matrix to be made.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Apr 23, 2015 at 3:41 PM, Erin Hodgess <erinm.hodgess at gmail.com>
wrote:

> Hello!
>
> I have a cbind type question, please:  Suppose I have the following:
>
> dog <- 1:3
> cat <- 2:4
> tree <- 5:7
>
> and a character vector
> big.char <- c("dog","cat","tree")
>
> I want to end up with a matrix that is a "cbind" of dog, cat, and tree.
> This is a toy example.  There will be a bunch of variables.
>
> I experimented with "do.call", but all I got was
> 1
> 2
> 3
>
> Any suggestions would be much appreciated.  I still think that do.call
> might be the key, but I'm not sure.
>
> R Version 3-1.3, Windows 7.
>
> Thanks,
> Erin
>
>
> --
> Erin Hodgess
> Associate Professor
> Department of Mathematical and Statistics
> University of Houston - Downtown
> mailto: erinm.hodgess at gmail.com
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list