How to concatenate vector with defined name to a list in R?


The concatenation of vector to a list simply means adding an external vector to the list we already have. For this purpose, we need to defined the vector with list function so that the R program understands that we are adding a list object to another list, otherwise the vector entries will be considered as separate vectors to be added in the list. To understand this in a better way, check out the below examples.

Example

 Live Demo

Consider the below list −

List1<-list(First=LETTERS[1:26],Second=1:50,Third=matrix(1:36,ncol=6),Fourth=runif(50,2,4))
List1

Output

$First
[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" "Y" "Z"

$Second
[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
[26] 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

$Third
    [,1] [,2] [,3] [,4] [,5] [,6]
[1,]  1    7    13   19   25   31
[2,]  2   8    14    20   26   32
[3,]  3   9    15    21   27   33
[4,]  4   10   16    22   28   34
[5,]  5   11   17    23   29   35
[6,]  6   12   18   24   30   36

$Fourth
[1] 3.105594 3.536182 2.822357 3.386584 2.734866 3.076056 2.452601 2.216502
[9] 2.688155 3.843628 2.933483 3.574460 2.387726 2.474020 2.881647 2.568924
[17] 3.409768 3.400333 3.091643 2.435296 3.682750 2.405809 3.275637 2.952360
[25] 2.128073 3.512086 3.901536 3.996337 2.627426 2.479627 2.608672 3.496575
[33] 2.175388 2.171842 3.970461 3.941115 2.679324 2.198789 3.077797 3.822259
[41] 2.755967 3.007756 2.335560 2.290525 3.633664 3.902446 3.075576 3.887534
[49] 2.230249 2.217131
Vector1<-rpois(50,5)

Concatenating Vector1 to List1 −

c(Preliminary=list(Vector1),List1) $Preliminary
[1] 6 2 5 6 7 1 7 5 2 5 7 5 4 2 9 7 5 8 6 8 2 3 6 6 10
[26] 3 6 6 10 9 5 2 4 7 5 6 1 5 6 3 9 6 2 1 5 4 5 1 4 4
$First
[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" "Y" "Z"
$Second
[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
[26] 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
$Third
    [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1    7    13   19   25   31
[2,] 2    8    14   20   26   32
[3,] 3    9    15   21   27   33
[4,] 4    10   16   22   28   34
[5,] 5    11   17   23   29   35
[6,] 6    12   18   24   30   36
$Fourth
[1] 2.368043 3.839085 2.775267 2.622687 3.857926 3.439802 3.631413 3.637745
[9] 3.017275 3.620918 2.107544 2.752775 2.726771 3.659143 2.395163 2.277998
[17] 2.399850 2.010679 3.648097 3.807787 3.047690 2.696860 2.524618 3.936629
[25] 3.031549 2.489504 3.540834 2.279883 2.597158 3.793984 2.935930 2.895894
[33] 2.695106 2.146260 2.883154 2.988391 3.689821 2.935294 2.728720 3.664533
[41] 2.583229 3.959209 2.538157 3.880645 2.796372 2.320823 3.977083 3.883078
[49] 2.147618 2.665910

Let’s have a look at another example −

List2<jxjv nb-list(S1=rexp(30,2.1),S2=rnorm(30,5,3),S3=rpois(30,2),S4=runif(30,5,10))
List2

$S1

[1] 1.54241391 0.09504450 0.39775714 0.61680879 0.51024838 0.84089015
[7] 0.67636346 0.25536840 0.03188332 0.68525119 0.43108825 0.54746807
[13] 0.71781262 0.41390016 0.16677722 0.09329755 0.18819858 1.68017309
[19] 0.62902891 0.45078280 0.40338857 0.50025056 0.50237619 0.45592656
[25] 0.06306543 0.16343975 0.24685925 0.39772357 0.77207224 0.74553481

$S2

[1] 2.3890449 2.9615717 6.3587234 2.1095865 4.7280973 3.8029657 8.0160899 [8] 7.0570182 2.3144050 1.9758513 7.9967678 2.7892204 6.6482545 9.6912383 [15] 3.9659230 6.6054653 5.4855250 4.5945647 5.2610201 7.5504387 2.6513214 [22] 4.4934729 0.9972057 2.8063221 8.3711995 4.2925001 0.7092825 6.1032578 [29] 3.1197122 7.2583224

$S3

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

$S4

[1] 6.800258 6.382982 8.705316 8.066745 5.453297 7.529251 7.832513 8.265122
[9] 5.395742 5.262823 9.301489 9.096554 5.729041 8.168480 9.346456 6.046878 [17] 6.038909 7.066232 8.682669 5.418149 5.519552 6.137480 6.318631 9.215192 [25] 7.798310 7.408167 6.087319 7.083038 6.602695 8.779171
Vector2<-round(runif(30,2,3))
c(Group=list(Vector2),List2)

$Group

[1] 2 3 2 3 2 2 2 2 2 2 3 2 2 2 2 3 3 2 3 2 2 2 2 2 2 3 2 3 2 2

$S1

[1] 1.54241391 0.09504450 0.39775714 0.61680879 0.51024838 0.84089015
[7] 0.67636346 0.25536840 0.03188332 0.68525119 0.43108825 0.54746807
[13] 0.71781262 0.41390016 0.16677722 0.09329755 0.18819858 1.68017309
[19] 0.62902891 0.45078280 0.40338857 0.50025056 0.50237619 0.45592656
[25] 0.06306543 0.16343975 0.24685925 0.39772357 0.77207224 0.74553481

$S2

[1]
2.3890449 2.9615717 6.3587234 2.1095865 4.7280973 3.8029657 8.0160899
[8] 7.0570182 2.3144050 1.9758513 7.9967678 2.7892204 6.6482545 9.6912383 [15] 3.9659230 6.6054653 5.4855250 4.5945647 5.2610201 7.5504387 2.6513214 [22] 4.4934729 0.9972057 2.8063221 8.3711995 4.2925001 0.7092825 6.1032578 [29] 3.1197122 7.2583224

$S3

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

$S4

[1] 6.800258 6.382982 8.705316 8.066745 5.453297 7.529251 7.832513 8.265122
[9] 5.395742 5.262823 9.301489 9.096554 5.729041 8.168480 9.346456 6.046878 [17] 6.038909 7.066232 8.682669 5.418149 5.519552 6.137480 6.318631 9.215192
[25] 7.798310 7.408167 6.087319 7.083038 6.602695 8.779171

Updated on: 07-Oct-2020

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements