Home > Blockchain >  Selecting multiple elements of the same name from a list in R
Selecting multiple elements of the same name from a list in R

Time:01-11

I have a long list (over ~180 items), and each item within the list has the same overall structure. I would like to select one element of that list from each item and make a new list out of it. How might I do this? See the following example:

Create a list out of 3 of the iris example dataset:

example <- list(iris, iris, iris)

A quick look at the structure:

str(example)

List of 3
 $ :'data.frame':   150 obs. of  5 variables:
  ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ :'data.frame':   150 obs. of  5 variables:
  ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ :'data.frame':   150 obs. of  5 variables:
  ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Say I wanted to pull Sepal.Width from each element of the list. How might I do this? I'm aware I could do something along the lines of example[c(1[Sepal.Width], 2[Sepal.Width], 3[Sepal.Width]), but with a long list something along these lines would be repetitive. Is it possible to perhaps use lapply() or a loop to pull these items out?

CodePudding user response:

You can do

lapply(example, `[[`, 'Sepal.Width')

Or

lapply(example, getElement, 'Sepal.Width')

Both of which return

#> [[1]]
#>   [1] 3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 3.7 3.4 3.0 3.0 4.0 4.4
#>  [17] 3.9 3.5 3.8 3.8 3.4 3.7 3.6 3.3 3.4 3.0 3.4 3.5 3.4 3.2 3.1 3.4
#>  [33] 4.1 4.2 3.1 3.2 3.5 3.6 3.0 3.4 3.5 2.3 3.2 3.5 3.8 3.0 3.8 3.2
#>  [49] 3.7 3.3 3.2 3.2 3.1 2.3 2.8 2.8 3.3 2.4 2.9 2.7 2.0 3.0 2.2 2.9
#>  [65] 2.9 3.1 3.0 2.7 2.2 2.5 3.2 2.8 2.5 2.8 2.9 3.0 2.8 3.0 2.9 2.6
#>  [81] 2.4 2.4 2.7 2.7 3.0 3.4 3.1 2.3 3.0 2.5 2.6 3.0 2.6 2.3 2.7 3.0
#>  [97] 2.9 2.9 2.5 2.8 3.3 2.7 3.0 2.9 3.0 3.0 2.5 2.9 2.5 3.6 3.2 2.7
#> [113] 3.0 2.5 2.8 3.2 3.0 3.8 2.6 2.2 3.2 2.8 2.8 2.7 3.3 3.2 2.8 3.0
#> [129] 2.8 3.0 2.8 3.8 2.8 2.8 2.6 3.0 3.4 3.1 3.0 3.1 3.1 3.1 2.7 3.2
#> [145] 3.3 3.0 2.5 3.0 3.4 3.0
#> 
#> [[2]]
#>   [1] 3.5 3.0 3.2 3.1 3.6 3.9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.7 3.0 3.4 3.1 2.3 3.0 2.5 2.6 3.0 2.6 2.3 2.7 3.0
#>  [97] 2.9 2.9 2.5 2.8 3.3 2.7 3.0 2.9 3.0 3.0 2.5 2.9 2.5 3.6 3.2 2.7
#> [113] 3.0 2.5 2.8 3.2 3.0 3.8 2.6 2.2 3.2 2.8 2.8 2.7 3.3 3.2 2.8 3.0
#> [129] 2.8 3.0 2.8 3.8 2.8 2.8 2.6 3.0 3.4 3.1 3.0 3.1 3.1 3.1 2.7 3.2
#> [145] 3.3 3.0 2.5 3.0 3.4 3.0

CodePudding user response:

We may use

library(purrr)
map(example, pluck, "Sepal.Width")

CodePudding user response:

A variation:

example <- list(iris, iris, iris)
sapply(1:3, function(x) example[[x]][[2]])
       [,1] [,2] [,3]
  [1,]  3.5  3.5  3.5
  [2,]  3.0  3.0  3.0
  [3,]  3.2  3.2  3.2
  [4,]  3.1  3.1  3.1
  [5,]  3.6  3.6  3.6
  [6,]  3.9  3.9  3.9

CodePudding user response:

Using lapply and output as data frame.

data.frame(lapply(example, function(x) x["Sepal.Width"]))
    Sepal.Width Sepal.Width.1 Sepal.Width.2
1           3.5           3.5           3.5
2           3.0           3.0           3.0
3           3.2           3.2           3.2
4           3.1           3.1           3.1
5           3.6           3.6           3.6
6           3.9           3.9           3.9
7           3.4           3.4           3.4
8           3.4           3.4           3.4
...
  • Related