2

Is it possible to convert a matrix contains 6 rows and 4 columns into 6 vectors, each row will be a vector.

m = matrix( c(2, 4, 3, 1, 5, 7,4,8,9,4,5,0,2,5,7,6,1,8), nrow=6,ncol=3)
 m
     [,1] [,2] [,3]
[1,]    2    4    2
[2,]    4    8    5
[3,]    3    9    7
[4,]    1    4    6
[5,]    5    5    1
[6,]    7    0    8
Noor
  • 365
  • 2
  • 13

3 Answers3

1

One option is split by the row of matrix to create a list of 'n' vectors where 'n' is the number of rows of the original matrix

lst <- split(m, row(m))

NOTE: It is better to create a list instead of having many objects in the global environment. Also, it is not clear why this is needed

akrun
  • 874,273
  • 37
  • 540
  • 662
1

You would try this example you can get the idea.

> b <- matrix(1:20, nrow = 2, ncol = 10)

> sapply(1:ncol(b), function(i) paste(b[,i],collapse=","))
 [1] "1,2"   "3,4"   "5,6"   "7,8"   "9,10"  "11,12" "13,14" "15,16"
 [9] "17,18" "19,20"
0

A solution with lapply:

lapply(as.data.frame(t(m)), c)
user1981275
  • 13,002
  • 8
  • 72
  • 101