# How to convert a list into an array in R?

R ProgrammingServer Side ProgrammingProgramming

To convert a list into an array then firstly we need to unlist the list elements and then use the array function. For example, if we have a list defined as List then it can be converted into an array using the command array(unlist(List)). Suppose the list contain the elements as shown below −

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

If we convert this list into an array then the output will be 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5.

## Example1

List1<−list(a=1:5,b=1:5,c=1:5)
List1
$a [1] 1 2 3 4 5$b
[1] 1 2 3 4 5
$c [1] 1 2 3 4 5 array(unlist(List1)) [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 ## Example2 Live Demo List2<−list(v1=rnorm(25,5,1),v2=rnorm(25,3,1),v3=rnorm(25,6,3.5),v4=rnorm(25,3,0.8)) List2$v1
[1] 4.569494 6.245254 5.824213 2.765431 3.737062 4.722491 4.596668 3.729680
[9] 5.194810 5.732013 5.654471 3.990308 5.463464 5.681799 4.200734 6.192203
[17] 4.935488 5.114750 5.538598 6.031840 5.638196 4.703505 6.250760 5.685816
[25] 5.033382
$v2 [1] 3.5336056 2.0668959 3.6471996 3.5726642 2.2213427 3.2938708 4.1728712 [8] 3.4768350 2.1376466 3.5188971 3.5907546 2.3617077 2.3917688 4.0631796 [15] 2.8022195 2.8992208 3.8937219 2.7219371 2.2742700 1.5346077 3.9733135 [22] 2.4182870 2.1741154 4.2095092 0.9747537$v3
[1] 10.4341829 8.1445983 10.5921786 8.7237160 8.0388067 1.3356382
[7] 1.1788600 0.2135948 9.0640978 4.4897520 9.6289285 4.9598040
[13] 9.9453925 6.8526298 6.8920683 2.4651608 7.9129389 6.9538885
[19] 10.2251289 8.0887021 11.5389208 2.6814165 3.3596456 6.3169849
[25] 12.7340040
$v4 [1] 3.0293395 2.1312382 4.4558115 3.3828561 2.4212923 0.5227674 3.8084856 [8] 3.7839529 3.2215775 2.3343886 2.8097641 3.0102862 3.4354359 4.6285038 [15] 2.8649354 2.7797428 3.0208512 3.3439515 1.1592660 3.1068066 3.2384929 [22] 2.6821956 4.3089607 2.0994566 2.1448611 array(unlist(List2)) [1] 4.5694944 6.2452537 5.8242134 2.7654310 3.7370622 4.7224910 [7] 4.5966678 3.7296805 5.1948095 5.7320132 5.6544711 3.9903082 [13] 5.4634635 5.6817992 4.2007339 6.1922026 4.9354882 5.1147505 [19] 5.5385982 6.0318395 5.6381965 4.7035053 6.2507603 5.6858160 [25] 5.0333817 3.5336056 2.0668959 3.6471996 3.5726642 2.2213427 [31] 3.2938708 4.1728712 3.4768350 2.1376466 3.5188971 3.5907546 [37] 2.3617077 2.3917688 4.0631796 2.8022195 2.8992208 3.8937219 [43] 2.7219371 2.2742700 1.5346077 3.9733135 2.4182870 2.1741154 [49] 4.2095092 0.9747537 10.4341829 8.1445983 10.5921786 8.7237160 [55] 8.0388067 1.3356382 1.1788600 0.2135948 9.0640978 4.4897520 [61] 9.6289285 4.9598040 9.9453925 6.8526298 6.8920683 2.4651608 [67] 7.9129389 6.9538885 10.2251289 8.0887021 11.5389208 2.6814165 [73] 3.3596456 6.3169849 12.7340040 3.0293395 2.1312382 4.4558115 [79] 3.3828561 2.4212923 0.5227674 3.8084856 3.7839529 3.2215775 [85] 2.3343886 2.8097641 3.0102862 3.4354359 4.6285038 2.8649354 [91] 2.7797428 3.0208512 3.3439515 1.1592660 3.1068066 3.2384929 [97] 2.6821956 4.3089607 2.0994566 2.1448611 ## Example3 Live Demo List3<−list(G1=LETTERS[1:8],G2=LETTERS[9:16],G3=LETTERS[17:24]) List3$G1
[1] "A" "B" "C" "D" "E" "F" "G" "H"
$G2 [1] "I" "J" "K" "L" "M" "N" "O" "P"$G3
[1] "Q" "R" "S" "T" "U" "V" "W" "X"
array(unlist(List3))
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X"

## Example4

Live Demo

List4<−list(Rate1=rpois(50,5),Rate2=rpois(50,7),Rate3=rpois(50,10),Rate4=rpois(50,2))
List4
$Rate1 [1] 5 9 7 1 3 5 4 3 1 6 6 3 7 3 4 3 3 4 7 9 4 5 2 6 7 [26] 11 5 8 3 7 0 4 6 2 4 6 5 7 6 7 5 8 3 6 4 7 6 8 4 5$Rate2
[1] 10 1 13 11 4 6 7 9 9 4 7 6 13 11 7 5 7 4 8 6 5 9 6 5 5
[26] 6 6 4 5 8 5 4 9 9 4 8 9 9 1 6 8 11 5 8 8 4 9 6 7 8
$Rate3 [1] 6 5 14 5 16 9 8 14 11 4 16 13 12 13 13 8 14 11 10 18 6 8 7 5 17 [26] 7 14 6 16 12 9 10 11 9 10 8 12 12 15 7 9 6 8 8 5 11 10 8 10 11$Rate4
[1] 3 3 1 3 4 4 2 1 1 1 0 2 3 2 0 2 2 2 0 0 1 0 1 0 3 3 0 3 2 1 1 4 0 2 0 3 2 5
[39] 3 3 2 3 0 3 1 3 3 0 1 2
array(unlist(List4))
[1] 5 9 7 1 3 5 4 3 1 6 6 3 7 3 4 3 3 4 7 9 4 5 2 6 7
[26] 11 5 8 3 7 0 4 6 2 4 6 5 7 6 7 5 8 3 6 4 7 6 8 4 5
[51] 10 1 13 11 4 6 7 9 9 4 7 6 13 11 7 5 7 4 8 6 5 9 6 5 5
[76] 6 6 4 5 8 5 4 9 9 4 8 9 9 1 6 8 11 5 8 8 4 9 6 7 8
[101] 6 5 14 5 16 9 8 14 11 4 16 13 12 13 13 8 14 11 10 18 6 8 7 5 17
[126] 7 14 6 16 12 9 10 11 9 10 8 12 12 15 7 9 6 8 8 5 11 10 8 10 11
[151] 3 3 1 3 4 4 2 1 1 1 0 2 3 2 0 2 2 2 0 0 1 0 1 0 3
[176] 3 0 3 2 1 1 4 0 2 0 3 2 5 3 3 2 3 0 3 1 3 3 0 1 2

## Example5

Live Demo

List5<−list(First=1:10,Second=21:50,Third=51:100)
List5
$First [1] 1 2 3 4 5 6 7 8 9 10$Second
[1] 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
[26] 46 47 48 49 50
\$Third
[1] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
[20] 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
[39] 89 90 91 92 93 94 95 96 97 98 99 100
array(unlist(List5))
[1] 1 2 3 4 5 6 7 8 9 10 21 22 23 24 25 26 27 28 29
[20] 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
[39] 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
[58] 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
[77] 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Updated on 06-Nov-2020 14:06:59