Skip to content

cbind question, please

16 messages · Michael Hannon, William Dunlap, Clint Bowman +10 more

#
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
#
Is this what you're looking for?
[,1] [,2] [,3]
[1,]    1    2    5
[2,]    2    3    6
[3,]    3    4    7

        
On Thu, Apr 23, 2015 at 3:41 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
#
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:

  
  
#
These are great! Thank you!
On Thu, Apr 23, 2015 at 7:14 PM, William Dunlap <wdunlap at tibco.com> wrote:

            

  
    
#
Perhaps:
dog cat tree
[1,]   1   2    5
[2,]   2   3    6
[3,]   3   4    7
dog cat tree
[1,]   1   2    5
[2,]   2   3    6
[3,]   3   4    7



Clint Bowman			INTERNET:	clint at ecy.wa.gov
Air Quality Modeler		INTERNET:	clint at math.utah.edu
Department of Ecology		VOICE:		(360) 407-6815
PO Box 47600			FAX:		(360) 407-7534
Olympia, WA 98504-7600

         USPS:           PO Box 47600, Olympia, WA 98504-7600
         Parcels:        300 Desmond Drive, Lacey, WA 98503-1274
On Thu, 23 Apr 2015, Erin Hodgess wrote:

            
#
This works for me...

get0 = function(x) get(x,pos=1)
sapply(big.char, get0)

The extra step seems necessary because without it, get() gets base::cat() instead of cat.

cheers,
    Steve

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Erin Hodgess
Sent: Friday, 24 April 2015 10:41a
To: R help
Subject: [R] cbind question, please

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
#
Hi Erin,
Well, if I do this:

dog <- 1:3
cat <- 2:4
tree <- 5:7
dct<-cbind(dog,cat,tree)

I get this:

dct
     dog cat tree
[1,]   1   2    5
[2,]   2   3    6
[3,]   3   4    7

If I assume that you want to include the character vector as well:

rownames(dct)<-big.char
dct

Jim
On Fri, Apr 24, 2015 at 8:41 AM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
#
What are you expecting?

dog <- 1:3
cat <- 2:4
tree <- 5:7
big.char <- c("dog","cat","tree")

xx <-  cbind(dog, cat, tree, big.char)

gives me 
xx1  <-  structure(c("1", "2", "3", "2", "3", "4", "5", "6", "7", "dog", 
"cat", "tree"), .Dim = 3:4, .Dimnames = list(NULL, c("dog", "cat", 
"tree", "big.char")))



John Kane
Kingston ON Canada
____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
#
Here is the big picture.  I have a character vector with all of the names
of the variables in it.

I want to "cbind" all of the variables to create a matrix.

Doing 3 is straightforward, but many, not so much.

Hence my question.

Thanks so much for your answers!

Sincerely,
Erin
On Thu, Apr 23, 2015 at 7:44 PM, John Kane <jrkrideau at inbox.com> wrote:

            

  
    
#
On 24/04/15 10:41, Erin Hodgess wrote:
I don't understand how you managed to get *that*.  When I did the 
"obvious" thing --- do.call(cbind,as.list(big.char)) --- I got
(as expected :-) )

      [,1]  [,2]  [,3]
[1,] "dog" "cat" "tree"
do.call(cbind,lapply(big.char,get,envir=.GlobalEnv))

Note:  I had to throw in the specification of "envir" otherwise get() 
got the cat() function from "base" rather than your vector "cat".

Probably not a problem for your real application; shouldn't hurt, but.

Another salutary example of why it's not a good idea to give data sets 
names that are names of R built-ins.

cheers,

Rolf
#
On Apr 23, 2015, at 5:41 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
Hi Erin,

One approach could be:
dog cat tree
[1,]   1   2    5
[2,]   2   3    6
[3,]   3   4    7

or
dog cat tree
[1,]   1   2    5
[2,]   2   3    6
[3,]   3   4    7


Note that I used the ?mode' argument to get(). You used ?cat? as the name of one of the objects and of course, there is an R function cat(). By default for get(), mode = ?any?, which would otherwise result in:
$dog
[1] 1 2 3

$cat
function (..., file = "", sep = " ", fill = FALSE, labels = NULL, 
    append = FALSE) 
{
    if (is.character(file)) 
        if (file == "") 
            file <- stdout()
        else if (substring(file, 1L, 1L) == "|") {
            file <- pipe(substring(file, 2L), "w")
            on.exit(close(file))
        }
        else {
            file <- file(file, ifelse(append, "a", "w"))
            on.exit(close(file))
        }
    .Internal(cat(list(...), file, sep, fill, labels, append))
}
<bytecode: 0x7fe942d78f78>
<environment: namespace:base>

$tree
[1] 5 6 7


In the above, the cat() function body is returned, instead of the vector cat. So just need to be cautious.

An alternative approach, depending upon where your vectors are stored, might be:
dog cat tree
[1,]   1   2    5
[2,]   2   3    6
[3,]   3   4    7


which specifies which environment to search for the named objects and the cat() function is not returned since it is in namespace:base.

See ?get

Regards,

Marc Schwartz
#
Hello Erin,

I think you have explain your goal more detailed. Maybe I am completely 
lost but as far as I understand now you only need the command cbind:

m1 <- cbind(dog, dat, tree)

  dog cat tree
[1,]   1   2    5
[2,]   2   3    6
[3,]   3   4    7

But I can't imagine that is the solution you are looking for.

Cheers
David
On 24.04.2015 00:41, Erin Hodgess wrote:

  
    
#
Hello,

I am not sure what you mean by a matrix. If you want to have a matrix, use the function matrix, (matrix(c(dog,cat,tree),3))
but I have the feeling you really want a data frame as you are talking about variables.
In that case simply use 

mydataframe <- data.frame(dog,cat,tree)

If you are not sure what you want, please read the intro to R pdf which is included in your installed R library.

Best regards,
daniel
________________________________________
Felad?: R-help [r-help-bounces at r-project.org] ; meghatalmaz&#243;: Erin Hodgess [erinm.hodgess at gmail.com]
K?ldve: 2015. ?prilis 24. 0:41
To: R help
T?rgy: [R]  cbind question, please

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


______________________________________________
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.
#
G'day Erin,

On Thu, 23 Apr 2015 20:51:18 -0400
Erin Hodgess <erinm.hodgess at gmail.com> wrote:

            
So I guess you want something like:

R> do.call(cbind, sapply(big.char, as.name))
     dog cat tree
[1,]   1   2    5
[2,]   2   3    6
[3,]   3   4    7

Which does not seem to have the problem of confusing the numeric vector
`cat' with the function 'cat'.

HTH.

Cheers,

	Berwin
#
I am amazed at the number of rather obtuse misunderstandings of the 
actual nature of Erin's question.

The suggestion that Erin should read the intro to R made me smile.  Erin 
is a long time and highly sophisticated user of R; she has no need to 
read the intro.  The person who made that suggestion should have read 
her question more thoughtfully.

Also I liked Steve Taylor's and Marc Swartz's nifty solutions that use 
sapply(); much sexier than my rather kludgy effort using do.call(). 
Berwin Turlach's combination of do.call() and sapply() is pretty sexy too.

cheers,

Rolf Turner
#
> This works for me...
    > get0 = function(x) get(x,pos=1)
    > sapply(big.char, get0)

Note that  get0() is a _ somewhat important for efficient code _
new function since R 3.2.0
so you'd rather call your functions differently...

    > The extra step seems necessary because without it, get() gets base::cat() instead of cat.

    > cheers,
    > Steve

    > -----Original Message-----
    > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Erin Hodgess
    > Sent: Friday, 24 April 2015 10:41a
    > To: R help
    > Subject: [R] cbind question, please

    > 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.

    > ______________________________________________
    > 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.