How to convert a list to matrix in R?


If we have a list that contain vectors having even number of elements in total then we can create a matrix of those elements. example, if a list contain 8 vectors and the total number of elements in those 8 vectors is 100 or any other multiple of 2 then we can create a matrix of those elements. This can be done by using unlist function inside matrix function.

Example

Consider the below list x −

> x<-list(1:25,26:50,51:75,76:100,101:125,126:150,151:175,176:200)
> x
[[1]]
  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

[[2]]
  [1] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[3]]
  [1] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

[[4]]
  [1] 76 77 78 79 80  81 82 83 84 85 86 87 88 89 90 91 92 93 94
 [20] 95 96 97 98 99 100
[[5]]
 [1] 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
[20] 120 121 122 123 124 125
[[6]]
 [1] 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[20] 145 146 147 148 149 150
[[7]]
 [1] 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
[20] 170 171 172 173 174 175
[[8]]
 [1] 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
[20] 195 196 197 198 199 200
> Matrix_x <- matrix(unlist(x), ncol = 10, byrow = TRUE)
> Matrix_x
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   1   2   3    4    5    6    7    8    9   10
 [2,]  11  12  13   14   15   16   17   18   19   20
 [3,]  21  22  23   24   25   26   27   28   29   30
 [4,]  31  32  33   34   35   36   37   38   39   40
 [5,]  41  42  43   44   45   46   47   48   49   50
 [6,]  51  52  53   54   55   56   57   58   59   60
 [7,]  61  62  63   64   65   66   67   68   69   70
 [8,]  71  72  73   74   75   76   77   78   79   80
 [9,]  81  82  83   84   85   86   87   88   89   90
[10,]  91  92  93   94   95   96   97   98   99  100
[11,] 101 102 103  104  105  106  107  108  109  110
[12,] 111 112 113  114  115  116  117  118  119  120
[13,] 121 122 123  124  125  126  127  128  129  130
[14,] 131 132 133  134  135  136  137  138  139  140
[15,] 141 142 143  144  145  146  147  148  149  150
[16,] 151 152 153  154  155  156  157  158  159  160
[17,] 161 162 163  164  165  166  167  168  169  170
[18,] 171 172 173  174  175  176  177  178  179  180
[19,] 181 182 183  184  185  186  187  188  189  190
[20,] 191 192 193  194  195  196  197  198  199  200
> Matrix_x <- matrix(unlist(x), ncol = 10)
> Matrix_x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]  1  21 41 61 81 101 121 141 161 181
 [2,]  2  22 42 62 82 102 122 142 162 182
 [3,]  3  23 43 63 83 103 123 143 163 183
 [4,]  4  24 44 64 84 104 124 144 164 184
 [5,]  5  25 45 65 85 105 125 145 165 185
 [6,]  6  26 46 66 86 106 126 146 166 186
 [7,]  7  27 47 67 87 107 127 147 167 187
 [8,]  8  28 48 68 88 108 128 148 168 188
 [9,]  9  29 49 69 89 109 129 149 169 189
[10,] 10  30 50 70 90 110 130 150 170 190
[11,] 11  31 51 71 91 111 131 151 171 191
[12,] 12  32 52 72 92 112 132 152 172 192
[13,] 13  33 53 73 93 113 133 153 173 193
[14,] 14  34 54 74 94 114 134 154 174 194
[15,] 15  35  55 75 95 115 135 155 175 195
[16,] 16  36 56 76 96 116 136 156 176 196
[17,] 17  37  57 77 97 117 137 157 177 197
[18,] 18  38  58 78 98 118 138 158 178 198
[19,] 19  39  59 79 99 119 139 159 179 199
[20,] 20  40  60 80 100 120 140 160 180 200

Updated on: 12-Aug-2020

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements