简体   繁体   中英

How to subset from a list in R

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.. Thanks in advance for your help

Mauricio Ortiz

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

mylist <- list(1:5, 6:10, 11:15)
sapply(mylist, "[", c(2,3))

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.

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)]})

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

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 )

Eg, 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)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM