# How to subset one or more sub-elements of a list in R?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

A list contains different type of elements and each of the them can have varying elements. To subset these sub-elements we can use sapply function and use c to subset the number of corresponding sub-elements. For example, if we have a list that contains five elements and each of those elements have ten sub-elements then we can extract 1, 2, 3 etc elements from sub-elements.

## Example

Consider the below list −

Live Demo

x1<-LETTERS[1:20]
x2<-sample(1:50,20)
x3<-rpois(20,5)
x4<-rnorm(20)
List<-list(x1,x2,x3,x4)
List

## Output

[[1]]
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T"
[[2]]
[1] 42 34 22 5 47 17 10 50 46 12 19 40 39 26 49 21 43 16 28 31
[[3]]
[1] 5 6 4 7 4 2 6 5 5 4 3 4 6 7 12 10 5 5 4 2
[[4]]
[1] 1.12451278 0.61614998 -0.92794642 0.65635700 2.42697739 1.27161636
[7] -0.64559672 0.08409151 -0.50431128 0.76662433 0.89623914 0.59773245
[13] 0.80662859 -2.56529294 0.56187367 0.54384064 -0.77754871 -1.67570110
[19] 0.21918446 1.04775386

Subsetting 1, 2, and 3 sub-element of List −

## Example

sapply(List,"[",c(1,2,3))

## Output

[,1] [,2] [,3]    [,4]
[1,] "A" "42" "5" "1.12451278481473"
[2,] "B" "34" "6" "0.616149983434114"
[3,] "C" "22" "4" "-0.927946420385339"

Doing the subsetting in similar manner −

## Example

sapply(List,"[",c(1:5))

## Output

[,1] [,2] [,3]    [,4]
[1,] "A" "42" "5" "1.12451278481473"
[2,] "B" "34" "6" "0.616149983434114"
[3,] "C" "22" "4" "-0.927946420385339"
[4,] "D" "5" "7" "0.656357003744367"
[5,] "E" "47" "4" "2.42697739177831"

## Example

sapply(List,"[",c(1:5,7))

## Output

[,1] [,2] [,3]    [,4]
[1,] "A" "42" "5" "1.12451278481473"
[2,] "B" "34" "6" "0.616149983434114"
[3,] "C" "22" "4" "-0.927946420385339"
[4,] "D" "5" "7" "0.656357003744367"
[5,] "E" "47" "4" "2.42697739177831"
[6,] "G" "10" "6" "-0.645596715535233"

## Example

sapply(List,"[",c(1,3,5,7,9,11,13,15,17,19))

## Output

[,1] [,2] [,3]      [,4]
[1,] "A" "42" "5" "1.12451278481473"
[2,] "C" "22" "4" "-0.927946420385339"
[3,] "E" "47" "4" "2.42697739177831"
[4,] "G" "10" "6" "-0.645596715535233"
[5,] "I" "46" "5" "-0.504311282950169"
[6,] "K" "19" "3" "0.896239139202067"
[7,] "M" "39" "6" "0.806628585616475"
[8,] "O" "49" "12" "0.561873666241118"
[9,] "Q" "43" "5" "-0.777548711788973"
[10,] "S" "28" "4" "0.219184464626918"

Let’s have a look at another example −

## Example

Live Demo

y1<-runif(50,5,10)
y2<-rexp(50,5)
y3<-rpois(50,5)
y4<-rbinom(50,10,0.5)
y5<-rnorm(50,2,5)
y6<-sample(1:10,50,replace=TRUE)
New_List<-list(y1,y2,y3,y4,y5,y6)
New_List

## Output

[[1]]
[1] 5.803367 6.089679 8.353850 6.553303 6.888362 9.339823 8.210219 9.192319
[9] 8.000832 6.570291 9.849118 5.782829 9.047976 8.780683 5.700685 8.056263
[17] 9.254796 9.025764 7.015314 5.377605 5.209807 9.203259 8.614667 9.482647
[25] 7.483367 5.337287 7.545549 5.533780 8.444888 8.343176 5.414887 5.977233
[33] 6.176886 5.207822 5.764009 6.368321 9.953098 6.155332 9.951671 8.708229
[41] 7.450868 5.054433 5.778852 7.402094 9.201947 5.729420 9.009465 6.462985
[49] 7.791356 9.183894
[[2]]
[1] 0.250019025 0.009724108 0.076594136 0.033039729 0.111726985 0.405952372
[7] 0.092882909 0.828488324 0.706934078 0.065782993 0.259104316 0.115519376
[13] 0.149173330 0.058378296 0.047328890 0.065369682 0.256626112 0.334837013
[19] 0.014795295 0.069345691 0.243743518 0.142329779 0.398242615 0.355403943
[25] 0.066698090 0.182716005 0.405173355 1.324708591 0.071726505 0.068690068
[31] 0.081411721 0.031406139 0.289635785 0.014949952 0.033212856 0.034007776
[37] 0.232678199 0.040067871 0.209419578 0.022348736 0.377370970 0.236765560
[43] 0.347200382 0.140650937 0.037227610 0.122573781 0.098753316 0.061054679
[49] 0.457099983 0.269695165
[[3]]
[1] 6 8 2 3 5 2 3 7 7 2 5 3 4 7 4 7 2 6 9 6 1 7 9 4 8 9 6 6 5 3 6 6 4 2 7 4 2 7
[39] 8 4 9 5 4 5 4 5 3 1 6 5
[[4]]
[1] 8 3 6 7 7 5 4 3 6 3 4 3 3 4 5 6 1 6 7 3 4 5 5 6 3 5 5 8 6 6 6 6 7 5 4 5 7 6
[39] 5 6 4 7 2 6 3 4 4 3 5 7
[[5]]
[1] 3.5555483 3.0150004 -2.3301461 -2.2768877 9.8853457 -0.3610741
[7] -0.8233241 -1.8213037 10.2790774 8.3444067 1.2147512 8.3433860
[13] -1.8563349 8.1101542 2.8981252 6.8148275 5.8198725 2.1595851
[19] 5.3660102 4.6578388 -1.8347568 -5.9426960 3.0356322 4.0896445
[25] 1.3474482 -0.6345953 5.2339709 -0.5880485 2.7042428 7.0862668
[31] 5.2520788 -1.4256616 6.8537118 -3.4600929 5.7622532 4.2848103
[37] 8.4462159 -1.6567224 -5.3483274 -1.6955801 -9.6959590 5.2263219
[43] 4.1314087 -7.7071415 -0.3637044 5.6304400 7.4015354 6.5949796
[49] 3.2027058 2.3075319
[[6]]
[1] 5 5 6 4 5 4 10 3 7 6 6 2 9 8 9 4 2 9 9 2 8 2 6 9 7
[26] 6 5 3 6 4 2 2 10 5 9 5 7 10 6 5 7 2 3 5 8 2 8 1 7 7

## Example

sapply(New_List,"[",c(1:10))

## Output

[,1]       [,2]    [,3] [,4]   [,5]       [,6]
[1,] 5.803367 0.250019025 6    8    3.5555483    5
[2,] 6.089679 0.009724108 8    3    3.0150004    5
[3,] 8.353850 0.076594136 2    6    -2.3301461   6
[4,] 6.553303 0.033039729 3    7    -2.2768877   4
[5,] 6.888362 0.111726985 5    7    9.8853457    5
[6,] 9.339823 0.405952372 2    5    -0.3610741   4
[7,] 8.210219 0.092882909 3    4    -0.8233241   10
[8,] 9.192319 0.828488324 7    3    -1.8213037   3
[9,] 8.000832 0.706934078 7    6    10.2790774   7
[10,] 6.570291 0.065782993 2    3    8.3444067   6
Updated on 08-Sep-2020 12:58:56