How to create a vector of lists in R?


If we have many lists but we want to use the values in the lists as a vector then we first need to combine those lists and create a vector. This can be done by using unlist function along with the combine function c to create the vector. For example, if we have two lists defined as List1 and List2 and we want to create a vector V using these lists then it can be created as:

V<-c(unlist(List1,List2))

Example1

Live Demo

> x1<-list(a=rnorm(10),b=rpois(10,2))
> x1

Output

$a
[1] -0.6972237 -1.5013768 -0.2451809 -0.2365569 -1.6304919 -1.1704378
[7] 1.1617054 -0.2349498 -1.2582229 0.4112065

$b
[1] 2 0 2 6 0 0 3 2 6 1

Example

Live Demo

> x2<-list(rnorm(10))
> x2

Output

[[1]]
[1] 0.2023410 -0.5211331 -1.6368447 0.9778100 -0.6328811 0.3910759
[7] 0.1794474 0.5075240 -0.1174491 -1.3090096

Creating a vector from list x1 and x2:

Example

> x<-c(unlist(x1,x2))
> x

Output

a1 a2 a3 a4 a5 a6 a7
-0.6972237 -1.5013768 -0.2451809 -0.2365569 -1.6304919 -1.1704378 1.1617054
a8 a9 a10 b1 b2 b3 b4
-0.2349498 -1.2582229 0.4112065 2.0000000 0.0000000 2.0000000 6.0000000
b5 b6 b7 b8 b9 b10
0.0000000 0.0000000 3.0000000 2.0000000 6.0000000 1.0000000

Example

> is.vector(x)

Output

[1] TRUE
>

Example2

Live Demo

> y1<-list(a=rnorm(50,1,0.04),b=rnorm(20,7,1.14),c=rnorm(25,114,31.47))
> y1

Output

$a
[1] 1.0441076 0.9709010 0.9949609 1.0019427 1.0384076 1.0169458 1.0036682
[8] 0.9913772 0.9528663 1.0180562 0.9363575 1.0732061 1.0307761 1.0422927
[15] 0.9860774 1.0709991 0.9987043 0.9890098 1.0506386 1.0613314 1.0187382
[22] 0.9772212 0.9851766 1.1028398 1.0009101 0.9986566 1.0576555 0.9099289
[29] 0.9717962 1.0827059 1.0047796 1.0217830 1.0003265 0.9670573 1.0053249
[36] 1.0276062 0.9628219 1.0086659 0.9886436 1.0223536 0.9989308 1.0481061
[43] 1.0536828 1.0523708 0.9183652 0.9704890 0.9572086 0.9998538 0.9375011
[50] 0.9276890

$b
[1] 6.079065 7.545869 6.823217 7.979219 6.403273 6.587805 6.433710 5.858546
[9] 7.745383 9.068145 6.817230 7.531164 6.807480 7.425329 6.753716 7.674849
[17] 5.945795 8.236862 8.325794 6.069669

$c
[1] 55.20174 145.56562 117.60680 140.48263 76.27128 108.36357 118.35613
[8] 119.83245 101.12617 138.44369 144.67036 114.76234 113.31759 85.17203
[15] 45.62139 151.66967 119.72055 110.41745 78.07230 138.55044 102.99897
[22] 98.48246 139.69139 27.23912 159.17110

Example

Live Demo

> y2<-list(q=rpois(50,4),w=rpois(20,7),r=rpois(25,5))
> y2

Output

$q
[1] 6 5 1 1 2 4 1 1 5 3 3 8 5 4 3 4 1 7 3 2 5 4 2 6 2 3 4 6 6 7 1 4 5 3 2 1 7 6
[39] 0 4 2 5 5 4 5 8 3 6 1 5

$w
[1] 9 9 5 7 10 10 5 5 3 10 7 4 6 10 11 4 12 9 12 9

$r
[1] 5 2 5 2 3 3 4 6 3 4 6 6 5 4 7 6 3 2 5 4 4 5 6 6 6

Example

> y<-c(unlist(y1,y2))
> y

Output

a1 a2 a3 a4 a5 a6
1.0441076 0.9709010 0.9949609 1.0019427 1.0384076 1.0169458
a7 a8 a9 a10 a11 a12
1.0036682 0.9913772 0.9528663 1.0180562 0.9363575 1.0732061
a13 a14 a15 a16 a17 a18
1.0307761 1.0422927 0.9860774 1.0709991 0.9987043 0.9890098
a19 a20 a21 a22 a23 a24
1.0506386 1.0613314 1.0187382 0.9772212 0.9851766 1.1028398
a25 a26 a27 a28 a29 a30
1.0009101 0.9986566 1.0576555 0.9099289 0.9717962 1.0827059
a31 a32 a33 a34 a35 a36
1.0047796 1.0217830 1.0003265 0.9670573 1.0053249 1.0276062
a37 a38 a39 a40 a41 a42
0.9628219 1.0086659 0.9886436 1.0223536 0.9989308 1.0481061
a43 a44 a45 a46 a47 a48
1.0536828 1.0523708 0.9183652 0.9704890 0.9572086 0.9998538
a49 a50 b1 b2 b3 b4
0.9375011 0.9276890 6.0790651 7.5458694 6.8232168 7.9792185
b5 b6 b7 b8 b9 b10
6.4032727 6.5878053 6.4337102 5.8585457 7.7453832 9.0681453
b11 b12 b13 b14 b15 b16
6.8172301 7.5311637 6.8074797 7.4253288 6.7537159 7.6748490
b17 b18 b19 b20 c1 c2
5.9457953 8.2368625 8.3257939 6.0696686 55.2017403 145.5656195
c3 c4 c5 c6 c7 c8
117.6067958 140.4826294 76.2712830 108.3635694 118.3561314 119.8324548
c9 c10 c11 c12 c13 c14
101.1261694 138.4436862 144.6703645 114.7623362 113.3175937 85.1720337
c15 c16 c17 c18 c19 c20
45.6213893 151.6696687 119.7205511 110.4174524 78.0722964 138.5504402
c21 c22 c23 c24 c25
102.9989677 98.4824591 139.6913875 27.2391168 159.1711013

Updated on: 21-Nov-2020

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements