 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract the names of list elements in R?
The names of list elements can be extracted by using the names function. For example, if we have a list defined as List that contains three elements with names element1, element2, and element3 then then these names can be extracted from the List by using the below command:
names(List)
Example1
> List1<-list(x1=rnorm(50),x2=rnorm(50,1,0.28),x3=rnorm(50,5,2.1)) > List1
Output
$x1 [1] -0.04518909 -0.22779868 0.24339595 -0.86189295 -0.73387277 -0.75313131 [7] 0.39694608 2.30565359 0.55670193 0.21973762 0.62968128 -0.90936921 [13] 1.33946741 -0.16315751 0.31357793 0.40365980 -0.23639612 -2.48749453 [19] 0.52152768 -1.57059863 0.51728464 0.98177111 0.65475629 0.23715538 [25] -0.71796609 -0.42731839 0.32335282 -0.90013122 -0.84549927 -0.88358214 [31] -0.32066379 -0.98945433 0.42469849 -1.63095343 0.32584448 0.10947333 [37] 0.23486625 0.28166351 1.18432843 0.94828212 0.09452671 0.56618262 [43] 0.52839533 1.22136242 0.76011311 0.11757345 2.47459913 -0.47890572 [49] -1.91918520 -0.02408708 $x2 [1] 0.8270975 0.9756136 1.1292744 1.0656721 0.7703431 0.8769942 1.1293051 [8] 1.0581120 1.0815165 1.2847627 0.6654920 1.2342987 0.6837453 1.2018498 [15] 1.0908703 1.1193189 0.5471691 1.0213345 0.8050452 0.4815627 0.6849457 [22] 0.8577580 1.1381807 1.0131761 1.2062184 0.7610613 1.3568787 0.9533896 [29] 1.2557551 0.4617964 0.8776077 1.3058164 1.1131230 1.2142810 0.9978407 [36] 0.7247716 1.4211604 0.5994001 0.7525555 1.4627048 0.7734563 0.8280289 [43] 0.9735345 1.0476358 1.1781612 0.8641444 1.0304007 0.6046310 0.8035452 [50] 0.7185106 $x3 [1] 2.617699 6.486300 3.796717 2.448953 8.561896 6.948493 6.174320 5.890213 [9] 6.333487 1.866408 4.988860 5.306587 6.862673 2.881893 2.172200 3.583765 [17] 3.830627 3.403771 6.404305 4.417270 4.538683 2.788276 4.869081 7.097852 [25] 2.967966 4.711434 5.088199 8.753127 5.219092 6.472948 3.698116 3.735600 [33] 5.301249 3.958626 2.951177 1.139676 5.973805 3.137057 5.817388 6.123433 [41] 5.380026 8.153839 4.524674 2.266312 2.914673 3.633610 5.324451 5.095410 [49] 5.221360 1.832700
Example
> names(List1)
Output
[1] "x1" "x2" "x3"
Example2
> List2<-list(x1=rpois(50,5),x2=rpois(50,1),x3=rpois(50,5),x4=rpois(50,8),x5=rpois(50,10)) > List2
Output
$x1 [1] 7 7 6 4 2 5 5 7 7 4 9 3 2 3 1 7 3 5 4 6 7 4 7 8 2 [26] 2 9 5 8 3 7 6 4 11 4 7 4 6 6 4 3 7 5 4 7 4 7 5 4 3 $x2 [1] 1 0 1 2 0 2 2 0 2 3 1 3 2 1 1 1 1 2 1 2 1 1 0 0 0 0 3 0 1 0 1 1 3 0 1 0 2 1 [39] 1 1 0 0 0 0 2 2 1 0 2 1 $x3 [1] 3 7 7 8 2 4 6 7 8 3 6 8 2 6 4 5 6 6 4 4 9 4 5 4 5 [26] 4 5 5 8 0 5 6 3 3 6 12 2 7 4 4 7 6 7 2 6 7 11 13 5 6 $x4 [1] 7 7 8 15 5 6 12 12 6 6 4 9 11 12 6 11 15 13 10 8 9 6 10 12 10 [26] 8 8 8 8 9 6 9 7 9 8 5 5 9 10 8 5 6 8 12 8 11 9 8 9 6 $x5 [1] 9 10 9 9 6 7 8 6 13 12 14 7 8 13 8 8 10 8 10 10 9 7 9 7 4 [26] 11 5 7 18 10 12 6 11 13 13 5 12 15 9 6 5 12 9 10 10 4 11 6 12 15
Example
> names(List2)
Output
[1] "x1" "x2" "x3" "x4" "x5"
Example3
> List3<-list(x1=runif(50,2,5),x2=runif(50,1,5),x3=runif(50,5,10)) > List3
Output
$x1 [1] 4.413341 3.318683 2.450609 3.117884 3.347430 2.167973 4.473528 3.308317 [9] 2.891431 3.259983 2.101081 2.489383 2.727202 4.103531 2.061813 2.126091 [17] 4.590316 4.890000 2.725137 4.086082 3.798446 2.682532 3.600169 4.228952 [25] 2.264763 4.732312 3.053867 2.090457 3.528672 3.097195 2.808085 4.666389 [33] 3.644389 4.517354 4.119483 4.859402 3.091996 2.886587 4.989477 2.945724 [41] 4.262805 2.757937 2.320727 3.989926 2.747881 3.432197 4.045197 2.313279 [49] 2.873494 4.004079 $x2 [1] 3.863761 3.060900 3.088654 1.429419 1.501166 3.371164 3.016494 2.433578 [9] 3.644674 2.931639 4.553203 1.309591 1.696542 4.647760 4.973822 3.630334 [17] 1.494571 2.141640 3.337546 3.793574 3.544146 3.906353 3.318471 1.653554 [25] 4.452764 2.863841 3.988762 4.696605 2.372169 3.913032 4.068302 4.211055 [33] 2.796389 4.569563 1.362797 1.360992 1.562173 3.539244 1.588572 2.740451 [41] 1.512468 2.503382 1.298349 1.508630 2.195898 4.040723 2.272298 4.225725 [49] 4.682531 3.030588 $x3 [1] 7.904580 9.373170 5.687662 5.615320 6.170858 9.833555 6.421968 8.256569 [9] 5.255653 9.225753 6.719730 9.973355 7.186546 6.466654 7.712851 7.791231 [17] 9.460951 8.485921 6.945822 6.276568 5.247020 8.141602 6.575557 7.804101 [25] 7.483326 7.482139 8.670358 5.600923 6.953811 8.090939 9.176059 7.189967 [33] 5.356993 8.398447 5.429305 7.351870 6.970362 6.780346 7.637896 8.434442 [41] 9.261975 5.176213 8.207120 5.733510 7.102484 7.958931 5.799031 7.223153 [49] 5.255746 5.394654
Example
> names(List3)
Output
[1] "x1" "x2" "x3"
Example4
> List4<-list(y1=rexp(50,1.12),y2=rexp(50,2.1)) > List4
Output
$y1 [1] 0.806148350 1.458187265 0.534007093 0.950131273 0.217697643 0.349654709 [7] 0.874155289 2.004103178 0.362298127 0.533686567 0.611537599 0.136836959 [13] 0.198465017 0.516533088 0.705214297 0.348598974 0.385648527 0.257238782 [19] 0.131113534 0.456706525 4.077398554 0.501530183 1.094008447 0.279010694 [25] 2.271581539 0.342538455 0.077849912 0.502802631 1.844349116 0.297917889 [31] 0.081726119 1.545759146 1.119755295 0.006217782 1.711755760 0.227284939 [37] 1.391966260 0.845130298 1.719655529 0.519416726 0.898552797 0.208143165 [43] 2.520577533 1.670047493 0.756593752 0.361542577 2.468861275 0.280330950 [49] 0.336729084 6.532385077 $y2 [1] 0.139012937 0.751480073 0.071020098 0.786773500 0.372965729 0.037590902 [7] 0.167700221 0.092755225 0.787360131 0.107568293 2.268001886 0.444335844 [13] 0.074739241 0.151111998 0.184172284 0.581389785 0.014686120 0.820748947 [19] 0.107117694 0.327100046 0.200850286 0.076522915 0.201419763 0.122467053 [25] 0.192721591 0.189551687 0.278166513 0.299343658 0.149084151 0.958924542 [31] 0.034754787 0.004645119 0.670121429 1.347222694 0.642560074 0.023936619 [37] 0.575837008 0.768578674 0.196833013 2.178089408 0.331298845 1.839747737 [43] 0.594427700 0.700286120 0.224447412 0.523475388 0.364124369 0.617526646 [49] 0.482226880 1.318312322
Example
> names(List4)
Output
[1] "y1" "y2"
Advertisements
 