How to extract the first n values of all elements of a list in R?

R ProgrammingServer Side ProgrammingProgramming

To extract the first n values of all elements of a list in R, we can follow the below steps −

• First of all, create a list.

• Then, use head function with sapply function to extract the first n values of all elements in the list.

Example 1

Create the list

Let’s create a list as shown below −

List1<-
list(v1=sample(1:100,150,replace=TRUE),v2=sample(1:100,150,replace=TRUE),v3=sample(1:100,150,replace=TRUE),v4=sample(1:100,150,replace=TRUE))
List1

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 $v1 [1] 22 60 21 92 95 24 76 48 86 4 76 42 68 45 59 53 36 48 [19] 38 8 76 13 7 73 49 76 92 10 97 34 8 77 78 68 77 5 [37] 9 14 87 9 80 86 82 51 71 41 63 77 5 10 56 37 28 37 [55] 47 27 1 50 85 47 71 74 51 26 98 35 30 71 69 99 69 34 [73] 100 29 97 97 98 61 65 89 56 76 45 67 97 62 53 45 8 47 [91] 38 75 78 20 6 6 38 15 17 97 43 28 18 66 87 72 83 94 [109] 51 8 49 100 43 65 65 100 69 15 41 58 16 60 98 52 82 68 [127] 57 79 16 38 85 26 30 62 94 34 65 1 90 36 55 95 77 63 [145] 11 25 63 87 19 51$v2
[1]   95 48 62  98  8 91 56 34  9 57  9 12 83 95 55 99 19 32
[19]  38 7  42 100 24  9 35 28 16 13 79 72 59 26 58 49  7 43
[37]  72 82 84  70 25 27 93 27 13 91 78 46 25 51 21 48 77 52
[55]  66 87 75  97 98 97 24 87 46 90  9 55 41 63 63  6 33 12
[73]  61 29 23  92 68 13 38 63 17 57 50 55 52 77 46 37 47 89
[91]  39 66 64  45 73 89 61 63 88 68 62 24 63 74 55 13 45  9
[109]  8 57 10  17 12 60 67 23 83 15 28 60 90 83 92 10 66  3
[127] 67 76 60  34 10 55 80 74 95 19 90 20 54 8 68 100  3 20
[145] 68 21 80  10 34 95

$v3 [1] 80 1 60 6 22 17 98 92 57 27 76 53 46 11 21 93 79 57 6 95 41 28 87 56 57 [26] 23 60 13 98 31 91 48 67 57 7 49 16 52 27 35 76 78 45 72 38 81 38 78 22 62 [51] 49 98 43 21 82 61 39 66 4 73 17 19 96 82 65 59 72 3 67 17 68 74 59 98 45 [76] 39 51 40 93 12 39 89 14 74 37 81 86 31 83 28 53 26 26 7 53 76 9 78 33 7 [101] 63 39 2 31 27 91 68 60 48 64 71 44 5 2 93 76 12 79 24 56 5 4 29 72 4 [126] 66 10 45 70 9 17 41 17 94 45 98 11 1 68 76 21 10 94 75 82 61 69 36 23 3$v4
[1]   12  72 69  5  4 49 84 74 93 24 50 14 22  76 31 51 96 26
[19]  48  68 39 94 44 26 32 56 82 64 77 84 85  15 66 10 18 49
[37]  49  29 84  4 36 78 29 60  7 74 48 38 73  18 53 39  8 55
[55]  73  62 42 81 36 91  3 16 87 98 61 35 43 100 82 12 65 48
[73]  28  86 15 30 61 49 87 55 14 64  1 83 54  46 92 53 31 63
[91]  55  23 29 71 59 50 26 30 81 15 19 16 97  51 54 49 44  9
[109] 69  22 99  9 45  3 37 42 17 87  8 84 13  26 68 47 88 88
[127] 54 100 12 24 89 38 66 24 75 33 23 68 61  68 81 56 94 45
[145] 71  26 23 38 7 93

Extract the first n values of all elements in the list

Using head function with sapply function to extract the first 10 values of all elements in List1 −

List1<-
list(v1=sample(1:100,150,replace=TRUE),v2=sample(1:100,150,replace=TRUE),v3=sample(1:100,150,replace=TRUE),v4=sample(1:100,150,replace=TRUE))
sapply(List1,head,10)

Output

      v1 v2 v3 v4
[1,]  79 15 21 26
[2,]  56 66 82  2
[3,]  8  19 96 68
[4,]  97 35 29 79
[5,]  97 98 55 78
[6,]  22 21 42 99
[7,]  69 55 44 74
[8,]  16 88 63 38
[9,]  69 69 30 69
[10,] 94 41 70 35

Example 2

Create the list

Let’s create a list as shown below −

List2<-list(x=rnorm(50),y= rnorm(50),z=rnorm(50))
List2

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 $x [1] -0.39792125 0.74964782 -0.57634257 0.71999035 0.32411962 -0.77093605 [7] -0.94714349 1.14963831 0.90029617 0.35403370 1.15488861 0.46021837 [13] 1.76129897 0.55533559 -0.99318917 0.02891958 0.17254889 -0.19344847 [19] 0.41305350 0.83138278 -0.38491469 0.41233018 0.74489232 0.02164781 [25] 0.35683122 -0.47193421 -0.86310810 0.29632447 -1.56675236 1.35308273 [31] -0.03833990 -0.91899995 -0.80964722 0.14063178 0.10355068 -0.42866679 [37] 0.63542371 -0.23414671 -1.30251102 -0.53564528 0.98143644 0.79836568 [43] 0.44999460 -0.11816612 1.15379010 -1.11469349 0.80081302 1.30714382 [49] 0.97439468 -0.11710697$y
[1]   0.46832227 -0.39366335 -0.15408130  0.74988733  0.14803404  0.01877236
[7]   1.74605539  1.37383353  0.08365044  0.74078162 -2.24731927  0.52644559
[13]  0.93709378 -1.04730770 -0.17765310  1.86340916  0.21534932  0.02788970
[19] -0.27614437  1.71926302  0.72573090  0.29402575 -0.54798205 -0.96702214
[25] -0.67154618 -1.56908490  1.79970814  1.89616649  0.57721665 -0.10516876
[31]  0.02550615  0.36972961 -0.45759932 -0.47377820 -1.04034276  0.81918878
[37] -0.16202697  0.77223396 -1.53104643  0.16054709 -0.84399144  0.78417853
[43]  0.60950313 -0.38649093  0.11035999  1.76338328  1.02102323  0.82569852
[49] -0.19150898  1.47684052

\$z
[1]   1.9119185569  1.2635075035 -0.8267127682 -1.4335086128 -1.0244723665
[6]  -0.5518066227  0.9751007326  0.3711138425  1.6393508714 -0.7550721589
[11] -0.8796699626 -0.7679678249  1.9115589659 -0.0039228629  1.6639555903
[16] -1.1242861240 -0.2465343092  0.8157478552  0.5552747962 -0.9389815629
[21]  1.0269967036  1.5574155633 -0.7315831725 -1.5702292613 -0.4221248517
[26]  1.2671640614 -0.8204813519  1.4164538836 -0.7008683226  0.2209475850
[31] -0.8890729405  0.8395892736 -1.5906412715 -0.4454429088  0.0367536375
[36] -0.4143328275  1.7596532115  1.4287741518 -0.7408800634 -0.6261697293
[41]  0.9819785012 -0.2516292546 -0.0953959014  2.1374435457  0.2729319888
[46]  1.2363266065 -0.5049998778  0.4441977383  0.0009051012 -0.5801065632

Extract the first n values of all elements in the list

Using head function with sapply function to extract the first 10 values of all elements in List2 −

List2<-list(x=rnorm(50),y= rnorm(50),z=rnorm(50))
sapply(List2,head,10)

Output

           x            y          z
[1,]  -0.599001268 -2.1302312  0.30960984
[2,]   0.979756782  0.1918245 -0.54982670
[3,]   1.843847852 -0.9411825 -1.76691486
[4,]   0.505655349 -1.8498407  0.42698123
[5,]  -0.564534267  1.5836994 -0.59171716
[6,]   0.155689792 -1.1136393  2.01242268
[7,]  -0.631630974 -0.2841103  0.99231845
[8,]   0.006202028  0.6883346  0.50758302
[9,]  -0.411402207  1.3089835 -1.54271187
[10,] -0.352348225  1.5188278  0.05449173
Published on 09-Nov-2021 05:20:44