How to extract first value from a list in R?

R ProgrammingServer Side ProgrammingProgramming

To extract first value from a list, we first need to access the element using double square brackets then the sub-element of each element will be accessed using single square brackets. For example, if we have a list called LIST containing five elements each having 10 elements then the first sub-element of the LIST will be selected by using LIST[[1]][1].

Example

 Live Demo

List1<-list(x1=rnorm(50),x2=rnorm(50))
List1

Output

$x1
[1] 0.11130222 0.16419755 -0.52346744 2.06291702 1.75401221 1.76745535
[7] 2.07647670 0.20604988 0.88090037 0.45002361 -0.41997558 0.13069306
[13] 1.03792793 0.60365638 -0.86619055 -0.94122172 0.64561932 0.50256648
[19] -0.61076372 0.90286990 -0.03448342 1.52897744 -0.12000996 0.07482846
[25] -1.12145357 0.76479988 0.59701522 1.46033578 -0.57620173 -1.17224749
[31] -0.73289297 -1.03691038 -1.28427938 -0.33767539 -1.01044720 0.22021704
[37] 0.67931259 1.79052238 -0.80291917 -0.21613130 0.58112656 0.74238488
[43] -0.37718519 1.31974664 2.33329258 0.33576900 -1.05233449 0.90282079
[49] -0.23821323 -2.18185932
$x2
[1] 0.34801958 -0.24730876 -0.64642026 -0.20068985 0.94328680 -0.35876719
[7] -0.65250327 -0.26434726 0.80161224 -1.98861577 -0.55784172 2.72095292
[13] -0.34377048 -1.41075225 -0.58016709 0.30349424 -0.10171845 -1.63588095
[19] 0.69116860 1.45962094 1.68120904 0.35287888 2.16297295 1.96956313
[25] 0.20567607 0.39924676 0.18837752 -2.34209766 -0.49420627 -0.19967231
[31] -0.06452823 -0.31314817 0.34765525 -0.31236504 -2.01858040 0.09302232
[37] -0.07158963 0.91019112 -0.74267435 -0.40049302 0.18465365 0.65059368
[43] 0.61031556 -0.23870247 -1.63562822 0.37430717 0.98758801 -0.09859013
[49] 0.29618676 0.66022211

List1[[1]][1]

[1] 0.1113022

List1[[2]][1]

[1] 0.3480196

Example

 Live Demo

List2<-list(y1=rpois(100,5),y2=rpois(100,5),y3=rpois(100,5),y4=rpois(100,5))
List2

Output

$y1
[1] 0 6 10 5 7 1 8 8 4 4 1 5 7 5 5 10 2 4 4 2 3 5 3 2 2
[26] 5 4 12 4 4 5 4 4 6 6 5 7 10 1 3 9 4 6 3 7 6 4 3 6 5
[51] 8 5 8 5 7 3 4 5 7 6 6 3 5 5 5 1 0 4 7 3 4 7 6 6 3
[76] 3 3 4 3 5 6 4 6 3 4 4 6 7 8 4 3 10 11 6 8 8 6 6 7 4
$y2
[1] 7 7 11 4 4 1 4 7 2 6 2 2 6 2 2 6 3 3 6 2 5 6 6 5 6
[26] 6 1 9 1 1 8 3 5 3 5 6 6 4 4 6 3 9 3 4 5 4 8 6 2 6
[51] 6 2 8 8 8 5 4 1 3 7 8 6 6 10 6 8 5 2 4 7 4 8 4 3 8
[76] 3 3 7 7 7 2 9 3 5 8 3 5 5 7 2 9 8 7 5 2 3 5 7 7 8
$y3
[1] 5 5 3 5 2 6 7 3 3 5 3 6 5 7 3 7 4 5 5 4 5 9 4 1 2
[26] 4 5 5 5 5 4 3 2 6 8 4 13 6 4 6 7 5 5 6 2 3 6 4 4 6
[51] 3 3 6 11 5 7 8 3 3 5 7 3 1 5 4 9 6 7 5 3 6 6 5 6 6
[76] 6 6 2 12 2 1 7 3 6 4 5 4 3 4 3 5 6 4 3 8 6 7 7 7 5
$y4
[1] 6 7 4 6 6 1 7 6 2 6 10 7 5 9 9 3 6 6 10 4 7 4 2 4 10
[26] 6 9 3 3 2 6 5 4 4 3 6 5 3 6 3 7 7 1 4 3 5 10 6 7 4
[51] 5 3 6 8 3 5 4 5 5 5 6 3 4 10 5 3 7 5 5 7 4 4 3 5 6
[76] 4 4 6 9 3 10 12 7 4 9 7 14 10 1 4 4 3 3 6 1 5 2 8 4 7

List2[[1]][1]

[1] 0

List2[[2]][1]

[1] 7

List2[[3]][1]

[1] 5

List2[[4]][1]

List2[[4]][1]

Example

 Live Demo

List3<-list(z1=rexp(50),z2=rexp(50),z3=rexp(50),z4=rexp(50))
List3

Output

$z1
[1] 1.718907646 0.790585280 0.204750764 1.203047129 0.189730356 0.265698841
[7] 0.471851353 1.178283257 0.278361849 1.725710894 0.004180386 1.500419515
[13] 0.253964796 1.118053750 0.350007993 0.068681637 0.069928887 0.271137326
[19] 0.316337873 1.513536640 0.608107488 0.100705733 0.091038117 1.680959338
[25] 3.098310359 0.024069859 0.079393005 0.676481508 1.677929247 0.528531744
[31] 0.952730628 0.605817827 0.552035683 0.179703399 0.029881320 1.763623732
[37] 0.251440660 0.489608381 0.343958400 0.680817402 2.488113521 0.182692466
[43] 1.838525970 0.026139385 1.336605331 0.187837140 1.001021679 1.068164745
[49] 0.586717864 0.188288941
$z2
[1] 0.96681400 0.93182169 1.06428806 0.14140707 0.39303890 1.00227481
[7] 0.02706789 2.80852463 1.71280020 1.03406754 1.00638814 0.70394098
[13] 2.02029292 1.21125497 0.82803622 1.84601814 1.67267708 1.62547079
[19] 1.43411676 1.07888560 0.26773645 1.22885541 1.12461358 0.97696734
[25] 0.04547523 0.19367367 0.29813403 1.17887043 0.22968845 0.69612129
[31] 0.60235370 3.02393213 2.27831540 2.85495798 0.57111993 3.15022562
[37] 1.40904829 3.62419435 4.06618871 1.93677916 0.50544569 1.53942186
[43] 4.00923268 0.06415687 0.16847399 0.95837914 2.17440413 0.69603180
[49] 0.32872915 0.87998219
$z3
[1] 0.03984708 0.37911072 0.39527617 1.11247389 0.45724934 1.47759184
[7] 0.90512793 0.35675484 0.88244845 0.12769840 0.00452113 0.53191616
[13] 0.33870747 1.47954421 1.21998604 0.76498694 1.25436621 3.86274192
[19] 0.86903165 1.02575073 3.26314757 0.96884011 2.99768499 0.73019254
[25] 0.81639460 0.69500919 1.32349089 2.55732287 0.86032776 0.21881670
[31] 0.23084453 0.01074831 0.48422758 0.48439722 1.38000094 0.30889465
[37] 1.19599421 0.60671956 1.16873709 0.99445705 0.74635728 0.40339360
[43] 1.32840395 2.92880480 1.04132163 1.68629929 0.25703192 0.68822688
[49] 0.92138168 0.43555544
$z4
[1] 0.019785280 0.332113114 0.603268548 1.475968243 0.404461872 0.084103244
[7] 0.802729027 1.684216058 2.004364217 1.100479145 0.268109553 2.453706887
[13] 0.716849180 0.045104141 0.386609886 0.149478953 1.209757102 0.941370074
[19] 1.726784369 0.681956061 0.427254268 0.830936060 0.692974098 0.716479172
[25] 1.624493349 0.216954219 0.384070517 0.516003236 2.268504498 0.750347184
[31] 2.898988536 0.022540033 0.069682582 0.324829354 0.005932369 1.836073771
[37] 0.608267033 1.554355547 0.664473031 0.228425454 1.815967761 0.046082996
[43] 0.865417744 0.063441784 0.015283203 0.517283282 4.257718179 0.228973207
[49] 0.870923003 1.105269648

List3[[1]][1]

[1] 1.718908

List3[[2]][1]

[1] 0.966814

List3[[3]][1]

[1] 0.03984708

List3[[4]][1]

[1] 0.01978528
raja
Published on 06-Feb-2021 13:38:56
Advertisements