How to extract first n values from each element in an R list?

R ProgrammingServer Side ProgrammingProgramming

When we large number of values in each element of a list in R, we might want to have a look at some top values to understand the data characteristics. For this purpose, we can extract first n values from each element in an R list by using lapply function along with head function as shown in the below given examples.

Example 1

Following snippet creates a list −

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

The following lists are created −

$x1
[1] -0.29412273 -0.32348123 0.35529551 0.18704827 -0.38327129 -0.79402439
[7] 1.06257001 -0.14685691 -2.38297097 2.21666998 -0.10559449 -0.56270566
[13] 0.89252255 0.40458504 0.17934336 -0.60127268 -0.38448660 -0.11107612
[19] -0.40441168 -0.78228507 0.28044635 -0.02117011 -0.26906978 -0.02647851
[25] -0.21109743 -0.20661665 -0.17562369 -0.92361895 -0.34122625 1.36611228
[31] -1.51115192 -1.21353850 -0.31170655 0.23080044 -1.53526780 0.16862594
[37] -0.31602374 -1.29689241 1.51843850 -0.18296048 1.51389417 -1.19380459
[43] 1.20534461 1.21706514 -0.16377691 0.35361642 -1.15659725 0.14219736
[49] -0.55583365 -0.08557621

$x2
[1] 0.311491622 -1.024142804 -0.809289385 -0.707023369 0.386459266
[6] -1.180765936 0.533545463 1.175164919 -0.496506504 -1.415301411
[11] 0.727172323 -1.287775672 -0.745113431 -2.017506037 0.403169746
[16] -0.597323213 0.846456309 0.027433595 0.761924708 1.777361681
[21] -0.346879638 -1.129480190 -0.385887130 -0.501701331 1.589285673
[26] 0.413919165 -0.313137503 -0.834603992 -0.299912141 -1.066794723
[31] -0.227198746 -0.765340190 0.235769673 -0.341782647 -0.459916439
[36] -0.577853497 0.124676412 -0.006837587 0.112722267 0.333351233
[41] -1.385502503 0.316369889 -0.272912281 1.833342382 -0.160239978
[46] 0.209978901 -1.790061919 -0.758664967 0.120407859 -0.665248818

$x3
[1] 1.18502154 -0.43979696 -1.69593395 0.07185898 -0.45473079 1.87549127
[7] 0.21882795 -0.16378654 -0.33556827 -0.30189373 0.10210572 1.91806965
[13] -1.19112557 -2.45616292 0.46416725 -0.89564733 0.07571364 0.79107502
[19] -0.47633618 -0.93571882 0.52597026 0.75574516 -1.30314666 0.97842438
[25] 0.67608835 0.30602605 -0.12453151 0.61164536 0.10977436 0.63132873
[31] -1.19420055 1.24198989 1.12299204 -1.84517330 -0.33905639 1.61470024
[37] -1.07232140 -1.06469515 -0.32599334 0.20202354 -0.11649034 1.01175607
[43] 0.24412630 0.02090365 -0.18170876 -0.05904491 0.07385048 0.29976498
[49] -1.40480329 1.25112329

$x4
[1] 1.19213315 1.40160250 1.65131223 -0.58392436 -0.43602159 1.34705231
[7] -1.50003363 -0.40035011 -1.82079939 -1.06244478 0.47741922 1.05508228
[13] -0.85336922 1.45168047 0.10913414 -0.66578342 -1.58023339 0.09244244
[19] -1.47534281 -0.85275606 0.38645669 -1.45036282 2.34699574 0.12058041
[25] 0.73825270 -1.85346600 0.40781162 -1.77723940 -0.16165908 1.36358471
[31] 0.67371657 0.54189716 -0.82450698 0.37847759 -1.30918920 -0.95221824
[37] -1.76484238 -0.44127379 -0.03935299 0.55137528 -0.14797748 -0.22414260
[43] 1.03232510 -0.44669548 -0.58068888 -1.51832820 0.61549303 0.97473313
[49] -0.24422464 -0.17811277

$x5
[1] 0.51693186 0.65020285 -1.81981789 -2.20330259 1.87550632 -1.45344331
[7] -0.56442661 0.18084585 1.44054003 -0.13837560 0.57252519 0.65156747
[13] 1.35336050 0.49567047 2.26884327 -0.29421190 0.72306910 -0.77230847
[19] -0.95256984 0.25196474 1.39844051 1.17584336 -0.53659444 0.13386472
[25] -0.18880505 1.04311105 -1.08580212 1.07161343 1.60837561 -0.05391171
[31] 0.20796652 -0.54320879 0.22606969 -1.08717645 -0.88413338 0.31265387
[37] -0.57126288 -0.05408016 0.37072741 -0.42639916 -0.76230979 0.34294913
[43] -0.70732982 0.91813127 -0.40278818 0.47861904 0.05142555 1.14683803
[49] 0.77067756 1.96043599

In order to extract top 20 values from each element of List1, add the following code to the above snippet −

List1<-list(x1=rnorm(50),x2=rnorm(50),x3=rnorm(50),x4=rnorm(50),x5=rnorm(50))
lapply(List1,head,n=20)

Output

If you execute all the above given snippets as a single program, it generates the following output −

$x1
[1] -0.2941227 -0.3234812 0.3552955 0.1870483 -0.3832713 -0.7940244
[7] 1.0625700 -0.1468569 -2.3829710 2.2166700 -0.1055945 -0.5627057
[13] 0.8925226 0.4045850 0.1793434 -0.6012727 -0.3844866 -0.1110761
[19] -0.4044117 -0.7822851

$x2
[1] 0.31149162 -1.02414280 -0.80928938 -0.70702337 0.38645927 -1.18076594
[7] 0.53354546 1.17516492 -0.49650650 -1.41530141 0.72717232 -1.28777567
[13] -0.74511343 -2.01750604 0.40316975 -0.59732321 0.84645631 0.02743359
[19] 0.76192471 1.77736168

$x3
[1] 1.18502154 -0.43979696 -1.69593395 0.07185898 -0.45473079 1.87549127
[7] 0.21882795 -0.16378654 -0.33556827 -0.30189373 0.10210572 1.91806965
[13] -1.19112557 -2.45616292 0.46416725 -0.89564733 0.07571364 0.79107502
[19] -0.47633618 -0.93571882

$x4
[1] 1.19213315 1.40160250 1.65131223 -0.58392436 -0.43602159 1.34705231
[7] -1.50003363 -0.40035011 -1.82079939 -1.06244478 0.47741922 1.05508228
[13] -0.85336922 1.45168047 0.10913414 -0.66578342 -1.58023339 0.09244244
[19] -1.47534281 -0.85275606

$x5
[1] 0.5169319 0.6502029 -1.8198179 -2.2033026 1.8755063 -1.4534433
[7] -0.5644266 0.1808458 1.4405400 -0.1383756 0.5725252 0.6515675
[13] 1.3533605 0.4956705 2.2688433 -0.2942119 0.7230691 -0.7723085
[19] -0.9525698 0.2519647

Example 2

Following snippet creates a list −

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

The following lists are created −

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

$y2
[1] 0 0 0 1 0 1 3 2 0 0 0 1 1 1 0 3 0 0 3 0 1 0 2 3 2 0 1 1 0 0 1 0 0 1 1 1 0
[38] 1 0 2 2 0 0 1 1 1 0 0 1 1 1 2 0 0 3 1 0 3 0 0 1 1 0 2 1 1 0 0 0 0 0 3 2 1
[75] 2 2 0 2 1 0 0 1 1 0 2 0 0 2 2 1 2 0 0 0 3 1 1 1 0 2

$y3
[1] 3 2 2 3 0 1 4 4 0 2 1 3 1 2 0 2 3 0 7 2 3 2 2 1 4 3 2 2 2 1 2 0 2 2 2 1 2
[38] 0 1 2 1 6 1 2 3 1 1 1 0 2 1 0 2 2 1 2 4 4 3 3 7 0 4 1 1 1 2 2 1 1 2 0 4 3
[75] 3 1 4 0 2 4 1 1 0 1 2 1 6 0 4 4 1 2 2 2 2 2 6 2 1 2

$y4
[1] 2 1 3 1 1 3 3 3 2 1 4 4 1 2 1 3 1 3 3 4 5 0 1 4 2 0 1 1 2 2 1 0 1 1 3 1 8
[38] 2 4 4 2 3 2 3 2 2 3 4 5 4 3 4 1 2 3 3 2 1 1 2 2 4 5 3 0 3 2 0 2 3 2 0 1 1
[75] 5 1 5 1 4 6 4 3 3 1 1 4 4 1 1 1 1 3 3 0 3 4 2 4 0 1

$y5
[1] 4 2 0 3 3 0 0 4 1 2 1 1 4 2 1 2 2 2 4 3 2 3 3 1 2 2 2 1 0 1 2 3 2 2 1 2 0
[38] 0 2 3 1 3 1 0 0 2 2 1 3 2 1 4 3 1 2 1 0 1 1 3 3 3 5 0 3 1 1 2 3 1 6 0 1 1
[75] 0 2 2 0 2 1 3 2 4 4 4 0 1 2 3 1 2 3 2 2 1 3 2 1 1 2

To extract top 20 values from each element of List2, add the following code to the above snippet −

List2<-list(y1=rpois(100,5),y2=rpois(100,1),y3=rpois(100,2),y4=rpois(100,2),y5=rpois(100,2))
lapply(List2,head,n=20)

Outpu

If you execute all the above given snippets as a single program, it generates the following output −

$y1
[1] 9 5 5 5 5 3 5 6 5 6 2 9 3 5 2 5 2 8 5 3

$y2
[1] 0 0 0 1 0 1 3 2 0 0 0 1 1 1 0 3 0 0 3 0

$y3
[1] 3 2 2 3 0 1 4 4 0 2 1 3 1 2 0 2 3 0 7 2

$y4
[1] 2 1 3 1 1 3 3 3 2 1 4 4 1 2 1 3 1 3 3 4

$y5
[1] 4 2 0 3 3 0 0 4 1 2 1 1 4 2 1 2 2 2 4 3
raja
Updated on 11-Nov-2021 08:14:08

Advertisements