user297850
user297850

Reputation: 8005

combine matrixes of vectors with different length together in R

I have two vectors,a and b. Both of them are of length 10. Then I combined them together into c. There is another vector d, which is of length 20. I got the error message when trying to combining them together. I would like to know is there any structure that can allow me to combine vectors with different length together.

> a<-rep(6,10)
> b<-rep(8,10)
> c<-cbind(a,b)
> d<-rep(10,20)
> c
> x<-cbind(c,d)
  Warning message:
  In cbind(c, d) :
  number of rows of result is not a multiple of vector length (arg 2)

Upvotes: 1

Views: 1186

Answers (2)

rocknaxe
rocknaxe

Reputation: 11

If you just want to combine two numeric vectors of unequal length, then use the c() operator:

a <- rep(6,10)
d <- rep(10,20)
c <- c(a,d)

In this case c is also a numeric vector:

> c
 [1]  6  6  6  6  6  6  6  6  6  6 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10

But if you want cbind() to work, then you have to make sure that "d" is small enough to be recycled:

a <- rep(6,10)
b <- rep(8,10)
c <- cbind(a,b)
d <- rep(10,5)
x <- cbind(c,d)

In this case, x is a matrix and vector "d" was recycled to match the length.

> x
      a b  d
 [1,] 6 8 10
 [2,] 6 8 10
 [3,] 6 8 10
 [4,] 6 8 10
 [5,] 6 8 10
 [6,] 6 8 10
 [7,] 6 8 10
 [8,] 6 8 10
 [9,] 6 8 10
[10,] 6 8 10

Upvotes: 0

bgoldst
bgoldst

Reputation: 35314

The issue here is that your c is not a vector. It's a matrix:

a <- rep(6,10);
b <- rep(8,10);
c <- cbind(a,b);
c;
##       a b
##  [1,] 6 8
##  [2,] 6 8
##  [3,] 6 8
##  [4,] 6 8
##  [5,] 6 8
##  [6,] 6 8
##  [7,] 6 8
##  [8,] 6 8
##  [9,] 6 8
## [10,] 6 8
c(typeof(c),mode(c),class(c));
## [1] "double"  "numeric" "matrix"

This is because you created it by cbind()ing two vectors together. cbind() means "column-bind", meaning it treats its operands as matrices or data.frames and smashes their columns together, so you end up with a wider matrix or data.frame. If both operands are vectors, they are treated as one-column matrices for this operation.

Now, actually, it is possible to cbind() a vector with a matrix, but the vector should have a length equal to the height (i.e. number of rows) in the matrix or data.frame. Demo:

d <- rep(10,10);
cbind(c,d);
##       a b  d
##  [1,] 6 8 10
##  [2,] 6 8 10
##  [3,] 6 8 10
##  [4,] 6 8 10
##  [5,] 6 8 10
##  [6,] 6 8 10
##  [7,] 6 8 10
##  [8,] 6 8 10
##  [9,] 6 8 10
## [10,] 6 8 10
cbind(d,c);
##        d a b
##  [1,] 10 6 8
##  [2,] 10 6 8
##  [3,] 10 6 8
##  [4,] 10 6 8
##  [5,] 10 6 8
##  [6,] 10 6 8
##  [7,] 10 6 8
##  [8,] 10 6 8
##  [9,] 10 6 8
## [10,] 10 6 8

Your warning message occurred because you created d as having length 20, when the matrix had height 10. The cbind() still technically succeeds (with the warning), but just truncates the new column-from-vector to be the same height as the matrix:

d <- rep(10,20);
cbind(c,d);
##       a b  d
##  [1,] 6 8 10
##  [2,] 6 8 10
##  [3,] 6 8 10
##  [4,] 6 8 10
##  [5,] 6 8 10
##  [6,] 6 8 10
##  [7,] 6 8 10
##  [8,] 6 8 10
##  [9,] 6 8 10
## [10,] 6 8 10
## Warning message:
## In cbind(c, d) :
##   number of rows of result is not a multiple of vector length (arg 2)

If you really want to have one data structure with vectors of different lengths, your only option is a list:

a <- rep(6,10);
b <- rep(8,10);
d <- rep(10,20);
list(a=a,b=b,d=d);
## $a
##  [1] 6 6 6 6 6 6 6 6 6 6
## 
## $b
##  [1] 8 8 8 8 8 8 8 8 8 8
## 
## $d
##  [1] 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10

Upvotes: 2

Related Questions