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