41

I have a rather simple task but haven't find a good solution.

> mylist  
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10

[[2]]
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"

[[3]]
[1] 25 26 27 28 29 30 31 32

y <- c(3,5,9)    

I would like to extract from mylist the sub-elements 3,5, and 9 of each component in the list. I have tried, sapply[mylist,"[[",y] but not luck!, and others like vapply, lapply, etc..

starball
  • 20,030
  • 7
  • 43
  • 238
user1257894
  • 671
  • 1
  • 7
  • 5

7 Answers7

40

You could use sapply(mylist, "[", y):

mylist <- list(1:5, 6:10, 11:15)
sapply(mylist, "[", c(2,3))
sgibb
  • 25,396
  • 3
  • 68
  • 74
28

Try using [ instead of [[ (and depending on what you're after you light actually want lapply).

From ?'[[':

The most important distinction between [, [[ and $ is that the [ can select more than one element whereas the other two select a single element.

joran
  • 169,992
  • 32
  • 429
  • 468
12

Using lapply:

# create mylist
list1<-1:10
list2<-letters[1:26]
list3<-25:32
mylist<-list(list1,list2,list3)

# select 3,5,9th element from each list
list.2 <- lapply(mylist, function(x) {x[c(3,5,9)]})
USER_1
  • 2,409
  • 1
  • 28
  • 28
5

purrr provides another solution for solving these kinds of list manipulations within the tidyverse

library(purrr)
library(dplyr)

desired_values <- c(1,3)

mylist <- list(1:5, letters[1:6], 11:15) %>% 
purrr::map(`[`,desired_values) 

mylist
zero323
  • 322,348
  • 103
  • 959
  • 935
DanO
  • 600
  • 3
  • 11
4

An easy way to subset repeated named elements of a list, similar to other answers here.

(so I can find it next time I look this question up)

E.g., subset the "b" elements from a repeating list where each element includes an "a" and "b" sub-element:

mylist <- list(
  list(
    "a" = runif(3),
    "b" = runif(1)
  ),
  list(
    "a" = runif(3),
    "b" = runif(1)
  )
)

mylist
#> [[1]]
#> [[1]]$a
#> [1] 0.7547490 0.6528348 0.2339767
#> 
#> [[1]]$b
#> [1] 0.8815888
#> 
#> 
#> [[2]]
#> [[2]]$a
#> [1] 0.51352909 0.09637425 0.99291650
#> 
#> [[2]]$b
#> [1] 0.8407162

blist <- lapply(
  X = mylist,
  FUN = function(x){x[["b"]]}
)

blist
#> [[1]]
#> [1] 0.8815888
#> 
#> [[2]]
#> [1] 0.8407162

Created on 2019-11-06 by the reprex package (v0.3.0)

Scransom
  • 3,175
  • 3
  • 31
  • 51
1

I don't think sgibb's answer gives what you would want. I suggest making a new function:

subsetList <- function(myList, elementNames) {
    lapply(elementNames, FUN=function(x) myList[[x]])
}

Then you can use it like this:

x <- list(a=3, b="hello", c=4.5, d="world")
subsetList(x, c("d", "a"))
subsetList(x, c(4, 1))

These both give

[[1]]
[1] "world"

[[2]]
[1] 3

which is what you would want, I think.

toby544
  • 111
  • 2
  • I have used this solution, but for some reason, it adds a column containing the index to each element. very confusing. – Swift Arrow Jan 16 '20 at 22:35
0

There are better ways of doing this, but here's a quick solution.

# your values
list1<-1:10
list2<-letters[1:26]
list3<-25:32

# put 'em together in a list
mylist<-list(list1,list2,list3)

# function
foo<-function(x){x[c(3,5,9)]}

# apply function to each of the element in the list
foo(mylist[[1]])
foo(mylist[[2]])
foo(mylist[[3]])

# check the output

> foo(mylist[[1]])
[1] 3 5 9
> foo(mylist[[2]])
[1] "c" "e" "i"
> foo(mylist[[3]])
[1] 27 29 NA
user1234357
  • 319
  • 1
  • 3