Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a matrix from multiple column vectors

Tags:

dataframe

r

How can I create a matrix from multiple column vectors?

I know that I can easily create a data frame with column vectors:

> colA <- 1:5
> colB <- 21:25
> colC <- 31:35
> data.frame(colA, colB, colC)
  colA colB colC
1    1   21   31
2    2   22   32
3    3   23   33
4    4   24   34
5    5   25   35

However, when I try matrix(), it gives me unexpected results, as shown below. How can create my desired matrix? I know I can do as.matrix(df), which nicely preserves the column names, but I'm looking for a more direct approach.

> matrix(colA, colB, colC)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
 [1,]    1    2    3    4    5    1    2    3    4     5     1     2     3
 [2,]    2    3    4    5    1    2    3    4    5     1     2     3     4
 [3,]    3    4    5    1    2    3    4    5    1     2     3     4     5
 [4,]    4    5    1    2    3    4    5    1    2     3     4     5     1
 [5,]    5    1    2    3    4    5    1    2    3     4     5     1     2
 [6,]    1    2    3    4    5    1    2    3    4     5     1     2     3
 [7,]    2    3    4    5    1    2    3    4    5     1     2     3     4
 [8,]    3    4    5    1    2    3    4    5    1     2     3     4     5
 [9,]    4    5    1    2    3    4    5    1    2     3     4     5     1
[10,]    5    1    2    3    4    5    1    2    3     4     5     1     2
[11,]    1    2    3    4    5    1    2    3    4     5     1     2     3
[12,]    2    3    4    5    1    2    3    4    5     1     2     3     4
[13,]    3    4    5    1    2    3    4    5    1     2     3     4     5
[14,]    4    5    1    2    3    4    5    1    2     3     4     5     1
[15,]    5    1    2    3    4    5    1    2    3     4     5     1     2
[16,]    1    2    3    4    5    1    2    3    4     5     1     2     3
[17,]    2    3    4    5    1    2    3    4    5     1     2     3     4
[18,]    3    4    5    1    2    3    4    5    1     2     3     4     5
[19,]    4    5    1    2    3    4    5    1    2     3     4     5     1
[20,]    5    1    2    3    4    5    1    2    3     4     5     1     2
[21,]    1    2    3    4    5    1    2    3    4     5     1     2     3
      [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
 [1,]     4     5     1     2     3     4     5     1     2     3     4     5
 [2,]     5     1     2     3     4     5     1     2     3     4     5     1
 [3,]     1     2     3     4     5     1     2     3     4     5     1     2
 [4,]     2     3     4     5     1     2     3     4     5     1     2     3
 [5,]     3     4     5     1     2     3     4     5     1     2     3     4
 [6,]     4     5     1     2     3     4     5     1     2     3     4     5
 [7,]     5     1     2     3     4     5     1     2     3     4     5     1
 [8,]     1     2     3     4     5     1     2     3     4     5     1     2
 [9,]     2     3     4     5     1     2     3     4     5     1     2     3
[10,]     3     4     5     1     2     3     4     5     1     2     3     4
[11,]     4     5     1     2     3     4     5     1     2     3     4     5
[12,]     5     1     2     3     4     5     1     2     3     4     5     1
[13,]     1     2     3     4     5     1     2     3     4     5     1     2
[14,]     2     3     4     5     1     2     3     4     5     1     2     3
[15,]     3     4     5     1     2     3     4     5     1     2     3     4
[16,]     4     5     1     2     3     4     5     1     2     3     4     5
[17,]     5     1     2     3     4     5     1     2     3     4     5     1
[18,]     1     2     3     4     5     1     2     3     4     5     1     2
[19,]     2     3     4     5     1     2     3     4     5     1     2     3
[20,]     3     4     5     1     2     3     4     5     1     2     3     4
[21,]     4     5     1     2     3     4     5     1     2     3     4     5
      [,26] [,27] [,28] [,29] [,30] [,31]
 [1,]     1     2     3     4     5     1
 [2,]     2     3     4     5     1     2
 [3,]     3     4     5     1     2     3
 [4,]     4     5     1     2     3     4
 [5,]     5     1     2     3     4     5
 [6,]     1     2     3     4     5     1
 [7,]     2     3     4     5     1     2
 [8,]     3     4     5     1     2     3
 [9,]     4     5     1     2     3     4
[10,]     5     1     2     3     4     5
[11,]     1     2     3     4     5     1
[12,]     2     3     4     5     1     2
[13,]     3     4     5     1     2     3
[14,]     4     5     1     2     3     4
[15,]     5     1     2     3     4     5
[16,]     1     2     3     4     5     1
[17,]     2     3     4     5     1     2
[18,]     3     4     5     1     2     3
[19,]     4     5     1     2     3     4
[20,]     5     1     2     3     4     5
[21,]     1     2     3     4     5     1
Warning message:
In matrix(colA, colB, colC) :
  data length [5] is not a sub-multiple or multiple of the number of rows [21]
like image 418
stackoverflowuser2010 Avatar asked Mar 06 '14 19:03

stackoverflowuser2010


1 Answers

You can use cbind to produce the desired matrix:

mat <- cbind(colA, colB, colC)
mat
#      colA colB colC
# [1,]    1   21   31
# [2,]    2   22   32
# [3,]    3   23   33
# [4,]    4   24   34
# [5,]    5   25   35
class(mat)
# [1] "matrix"

You don't get the matrix you're expecting with the call of matrix(colA, colB, colC), because your arguments are getting interpreted as the first, second, and third arguments to the matrix function (aka data, nrow, and ncol). If you wanted to use the matrix function, you would need to provide your data as a single argument, with something like mat <- matrix(c(colA, colB, colC), ncol=3). If you used this syntax, you would not get the column names from the variables like we did with cbind.

like image 183
josliber Avatar answered Oct 29 '22 07:10

josliber