Search code examples
rvectorconcatenation

Why does cbind give a different output to c when two numbers are used as inputs?


Consider the following outrageously simple example:

> c(42,50)
[1] 42 50
> cbind(42,50)
     [,1] [,2]
[1,]   42   50

Clearly, c and cbind are giving different outputs. But why is this? For a simple case like this, why aren't both of their approaches to concatenation the same? What's the technical difference between their outputs? I was expecting both to be numeric vectors of length two, but cbind's output is clearly something else.


Solution

  • Just do ?cvs ?cbind :

    c                     package:base                     R Documentation
    
    Combine Values into a Vector or List  
    

    vs

    cbind                   package:base                   R Documentation   
    
    Combine R Objects by Rows or Columns
    

    they are, by definition, not the same.

    In other words, I see no reason to expect the same output. That being said, here's how you could get the same structure just in case :

    > str(as.vector(cbind(42,50)))
      num [1:2] 42 50
    

    vs

    > str(c(42,50))  
      num [1:2] 42 50