How to convert matrix rows into a list in R?

R ProgrammingServer Side ProgrammingProgramming

Depending on our objective, a matrix rows might be needed to converted into a list that means each row will be an element of the list. This can be done by using the function as.list but firstly we need to convert the matrix into data frame after transposing. For example, if we have a matrix called M then it’s rows will be converted to a list using the command writtem below −

as.list(data.frame(t(M)))

Example1

 Live Demo

M1<−matrix(1:25,nrow=5)
M1

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 1    6   11   16   21
[2,] 2    7   12   17   22
[3,] 3    8   13   18   23
[4,] 4    9   14   19   24
[5,] 5   10   15   20   25

Example

as.list(data.frame(t(M1)))

Output

$X1
[1] 1 6 11 16 21
$X2
[1] 2 7 12 17 22
$X3
[1] 3 8 13 18 23
$X4
[1] 4 9 14 19 24
$X5
[1] 5 10 15 20 25

Example2

 Live Demo

M2<−matrix(rnorm(36),nrow=6)
M2

Output

       [,1]       [,2]      [,3]        [,4]        [,5]         [,6]
[1,] 1.320181 0.17152409 −1.76634091 −0.48085533   −1.6186915   −1.4374249
[2,] −0.220481 −0.31776877 −2.16503900 −1.04481201 −1.2440593   −0.5943860
[3,] −0.138843 1.24491033 −0.23453374 0.09363227   0.1624981    −2.2085699
[4,] −0.535679 1.05556228 1.27692225 −1.00761472   −0.6388036    0.2438138
[5,] 1.563999 −0.08637959 −0.10161734 −3.03514376  −0.2729878 −0.4657192
[6,] 1.252191 2.09451091 −0.04484482 −0.96138526   1.2211188    −0.5685082

Example

as.list(data.frame(t(M2)))

Output

$X1
[1] 1.3201811 0.1715241 −1.7663409 −0.4808553 −1.6186915 −1.4374249
$X2
[1] −0.2204810 −0.3177688 −2.1650390 −1.0448120 −1.2440593 −0.5943860
$X3
[1] −0.13884303 1.24491033 −0.23453374 0.09363227 0.16249808 −2.20856995
$X4
[1] −0.5356790 1.0555623 1.2769222 −1.0076147 −0.6388036 0.2438138
$X5
[1] 1.56399912 −0.08637959 −0.10161734 −3.03514376 −0.27298782 −0.46571917
$X6
[1] 1.25219067 2.09451091 −0.04484482 −0.96138526 1.22111884 −0.56850819

Example3

 Live Demo

M3<−matrix(rpois(16,2),ncol=4)
M3

Output

   [,1] [,2] [,3] [,4]
[1,] 1    2    2    2
[2,] 0    5    1    3
[3,] 1    4    3    1   
[4,] 0    1    4    2

Example

as.list(data.frame(t(M3)))

Output

$X1
[1] 1 2 2 2
$X2
[1] 0 5 1 3
$X3
[1] 1 4 3 1
$X4
[1] 0 1 4 2

Example4

 Live Demo

M4<−matrix(runif(25,2,10),nrow=5)
M4

Output

        [,1]     [,2]     [,3]     [,4]    [,5]
[1,] 9.470178 3.844069 3.126983 7.666309 8.801446
[2,] 4.517685 6.602599 6.353498 9.570915 2.854464
[3,] 5.104221 5.793673 4.875067 6.810684 2.926270
[4,] 2.694206 4.677686 8.749031 6.201677 4.149385
[5,] 2.085755 9.849269 9.084727 3.010815 7.153298

Example

as.list(data.frame(t(M4)))

Output

$X1
[1] 9.470178 3.844069 3.126983 7.666309 8.801446
$X2
[1] 4.517685 6.602599 6.353498 9.570915 2.854464
$X3
[1] 5.104221 5.793673 4.875067 6.810684 2.926270
$X4
[1] 2.694206 4.677686 8.749031 6.201677 4.149385
$X5
[1] 2.085755 9.849269 9.084727 3.010815 7.153298

Example5

 Live Demo

M5<−matrix(rexp(16),nrow=4)
M5

Output

       [,1]      [,2]       [,3]    [,4]
[1,] 1.4500225 1.2780952 3.8074267 0.5150933
[2,] 0.6914497 3.0919047 4.9547217 1.5925355
[3,] 0.6407890 0.1222374 1.3573294 1.1416469
[4,] 1.0918338 1.1835358 0.8053099 0.2225276

Example

as.list(data.frame(t(M5)))

Output

$X1
[1] 1.4500225 1.2780952 3.8074267 0.5150933
$X2
[1] 0.6914497 3.0919047 4.9547217 1.5925355
$X3
[1] 0.6407890 0.1222374 1.3573294 1.1416469
$X4
[1] 1.0918338 1.1835358 0.8053099 0.2225276
raja
Published on 05-Feb-2021 10:14:18
Advertisements