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

R ProgrammingServer Side ProgrammingProgramming

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

  • First of all, create a list.

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

Example 1

Create the list

Let’s create a list as shown below −

List1<-list(v1=rnorm(50),v2=rnorm(50),v3=rnorm(50),v4=rnorm(50))
List1

Output

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

$v1
 [1] -0.329223160 -0.345938441 -0.782534244  0.106924619 -0.494456509
 [6]  0.758347069  0.015722368 -0.138039612  1.596068610  1.446460755
[11] -0.628530894 -0.218582626  0.008163983 -0.501272224 -1.764751740
[16]  1.710383478  1.169141833  0.557281055  1.262848553  2.024126740
[21] -0.852927602 -1.211188695 -0.441880820 -1.206217921 -0.797646563
[26] -0.967059738 -1.395283571 -0.098682041  0.451534648  0.642313947
[31] -0.932702753 -0.565765092  1.783998547  1.533496141  1.786930703
[36] -0.925787866  1.088374868  1.226852811 -0.261073401 -0.806575473
[41] -0.134762187 -1.128852338 -1.377805948  0.367587618 -0.220392434
[46] -0.016853901 -2.461667388  1.534953135  0.761156418  0.176827166
$v2
 [1] -0.76099841  0.86771800 -0.63181854 -0.51631398 -2.53535239 -0.70968787
 [7] -1.21191069 -0.96709946  0.96159762  1.81099044 -0.99090458  0.06723956
[13] -0.35698980  0.49001623 -0.11009238  0.55431489 -1.06048023  1.48361978
[19]  0.54117661 -0.54153867  0.77857859  1.29583082 -0.33251194  0.11096752
[25] -1.27335075  0.36533059 -0.17898800  2.22118426 -0.40835479 -0.03293850
[31]  0.92731420  1.73639400  1.00053948  1.61819404  0.42900488  1.89540248
[37]  0.01959906  1.35908615 -0.59017510 -0.37152636 -0.65929794  1.26330276
[43] -1.11545727 -0.51687445  0.39558303 -2.88120088  0.69623946 -0.71496741
[49] -1.42748758 -0.27860230
$v3
 [1] -0.24000676  1.06523149 -2.14495028  0.17865037  0.37816840 -0.80573760
 [7]  0.90013593 -0.83107406  1.05407018  0.43395124  1.17628814  1.29173112
[13] -0.56485152 -2.80729650  0.78636837 -0.37919322 -0.09312585  1.46810227
[19]  1.43798664  0.92268563  0.10122861  0.58440289 -1.62562346  1.10598635
[25]  0.60992245  0.70294428  0.92639712 -0.10686199 -0.58975841 -0.68471500
[31] -0.58895744 -0.35081472  0.23719477 -0.16315963 -0.61086616  0.40839189
[37] -0.05746384 -1.57600113  2.37700458  0.91087399 -1.31715348  0.32757144
[43] -1.14353590  0.07380687 -0.92749195 -0.99620201  0.34564712 -1.04244193
[49] -0.19390489  0.33184937
$v4
 [1] -1.56940261  0.91242445  1.06215295  0.24439339 -0.18742524  0.17557542
 [7]  1.64408674 -0.25826879  0.55959326 -0.33062472 -0.36383674  0.41828547
[13] -0.19863830  0.77664560 -1.32160811 -1.75454687  0.73355965 -0.19907927
[19]  0.98342758  0.21656616  0.22308005  0.17787506  0.54880953 -0.44581818
[25]  0.77578620  3.00871099  1.38691178  0.45234333 -0.70813049 -0.22781098
[31] -0.57699962  1.05857575  0.82046234  0.16887100  1.27772419  0.96839414
[37] -0.12340026  0.24655061 -1.75160008 -2.61150272 -1.39167654  0.07710894
[43]  0.54528189 -0.10825879 -1.47898170 -0.53123971 -0.81399129 -0.23731978
[49]  0.23258015 -0.79579865

Extract the last n values of all elements in the list

Using tail function with sapply function to extract the last 10 values of all elements in List1 −

List1<-list(v1=rnorm(50),v2=rnorm(50),v3=rnorm(50),v4=rnorm(50))
sapply(List1,tail,10)

Output

          v1        v2          v3          v4
[1,] -0.1347622 -0.6592979 -1.31715348 -1.39167654
[2,] -1.1288523  1.2633028  0.32757144  0.07710894
[3,] -1.3778059 -1.1154573 -1.14353590  0.54528189
[4,]  0.3675876 -0.5168744  0.07380687 -0.10825879
[5,] -0.2203924  0.3955830 -0.92749195 -1.47898170
[6,] -0.0168539 -2.8812009 -0.99620201 -0.53123971
[7,] -2.4616674  0.6962395  0.34564712 -0.81399129
[8,]  1.5349531 -0.7149674 -1.04244193 -0.23731978
[9,]  0.7611564 -1.4274876 -0.19390489  0.23258015
[10,] 0.1768272 -0.2786023  0.33184937 -0.79579865

Example 2

Create the list

Let’s create a list as shown below −

List2<-list(x=rpois(200,5),y=rpois(200,2),z=rpois(200,10))
List2

Output

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

$x
 [1]  6  4 2 2 5 11  3 8 9  3  4 7 4 4 4 10  5 3 5 4 3 9 2 6  3
[26]  7  5 4 5 8  3  4 6 6  5 11 1 2 6 7  5  8 5 6 3 4 6 3 5  1
[51]  8  1 6 4 2  5  8 7 5  5  5 6 4 7 1  4  4 4 0 6 6 5 6 6  1
[76]  3  5 9 4 5  1  7 5 4 10  8 4 5 4 4  3  6 3 4 7 8 2 3 4  4
[101] 1 12 4 7 4  7  5 5 5  4  8 5 5 2 8  7  4 2 5 5 3 4 6 6  5
[126] 9  4 4 6 5  4  5 6 5  4  4 9 5 6 4  5  7 4 4 5 6 4 3 4  0
[151] 6  4 7 7 5  2  6 7 3  4  7 8 5 9 4  5  2 8 5 4 3 3 4 4 12
[176] 8  2 4 4 5  3 13 2 2  6  2 7 2 4 6  2 10 4 6 5 2 2 4 8  4
$y
 [1]  1 1 2 2 2 2 2 2 2 2 1 3 0 4 3 4 3 2 1 2 3 3 2 2 2 4 2 2 4 2 2 1 2 5 2 1 3
[38]  1 0 2 1 3 4 2 0 1 3 2 3 4 3 3 3 5 0 1 2 2 5 5 1 2 3 2 2 3 1 5 1 4 0 5 1 0
[75]  2 1 2 1 1 2 1 2 0 4 1 2 0 4 4 6 0 8 2 5 0 2 2 1 3 4 2 2 0 1 1 1 1 3 4 3 5
[112] 3 1 4 1 3 0 1 1 4 4 1 1 3 1 1 4 0 0 5 1 0 2 1 2 2 2 2 1 0 0 0 5 0 2 2 5 1
[149] 0 2 3 4 4 1 2 3 1 3 1 1 1 3 1 1 0 0 1 2 2 1 1 1 4 2 1 1 1 1 2 1 2 3 2 3 2
[186] 2 1 2 3 2 2 1 3 3 3 1 3 0 1 3
$z
[1]   15 12 15 18  8  6 12  8 17 10  8 11  9  9 11  3 11  2  6 13  6 18 15  8 11
[26]  11  8 12  7 14 14 10 13 10 14 11  8 14  7 16  5 13 14  9 10 11  8 12  9 11
[51]   4  7 10  7  8  7 14 10  8 11  3  7  6  3  7 12  9  9  8  8 12  8 10 11  6
[76]  11 10 15  9  9  8  7 17 10 11 12  9  5 16  9  9  9  7  6  9 11 12  6 12  9
[101] 11  6  7  9 10  7  8 14 15  5 13 12  9 10 16  9 10 12  9 13  9  9  7 15  7
[126] 14 10  5 11  5 10 13  8  6 11 10  7 13 11 15 12  7 10  8  6  6  8  6 10  9
[151]  8 10 16 11  7 10 13 10  9 11 14 11  8  9  6  9 12  6 10 11  9 13 12 12 10
[176] 11 10 13  9 11  2  9  8  7  8 11  6  7  7 16 14 13 18  7 19  5  9  9  8 13

Extract the last n values of all elements in the list

Using tail function with sapply function to extract the last 10 values of all elements in List2 −

List2<-list(x=rpois(200,5),y=rpois(200,2),z=rpois(200,10))
sapply(List2,tail,10)

Output

      x y z
[1,]  2 2 14
[2,] 10 1 13
[3,]  4 3 18
[4,]  6 3  7
[5,]  5 3 19
[6,]  2 1  5
[7,]  2 3  9
[8,]  4 0  9
[9,]  8 1  8
[10,] 4 3 13
raja
Published on 16-Nov-2021 06:05:22
Advertisements