Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从R中的列表中进行子集_R_List_Sapply - Fatal编程技术网

如何从R中的列表中进行子集

如何从R中的列表中进行子集,r,list,sapply,R,List,Sapply,我有一个相当简单的任务,但还没有找到一个好的解决方案 > 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)

我有一个相当简单的任务,但还没有找到一个好的解决方案

> 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)    
# 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
我想从mylist中提取列表中每个组件的子元素3、5和9。 我已经试过了,Sappy[mylist,[[y],但没有运气!,还有其他像Vappy、lapply等。提前感谢您的帮助

毛里西奥·奥尔蒂斯(Mauricio Ortiz)

试着用[而不是[[并且取决于你真正想要的是什么]

从?“[]”:

[,[]和$之间最重要的区别是,[可以 选择多个元素,而其他两个选择单个元素 元素

您可以使用sapplymylist,[,y:


有更好的方法可以做到这一点,但这里有一个快速的解决方案

> 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)    
# 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
使用lappy:

为解决tidyverse中的此类列表操作提供了另一种解决方案

library(purrr)
library(dplyr)

desired_values <- c(1,3)

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

mylist

我不认为sgibb的答案能满足您的需求。我建议您创建一个新功能:

subsetList <- function(myList, elementNames) {
    lapply(elementNames, FUN=function(x) myList[[x]])
}
我想这就是您想要的。

一种简单的方法,可以将列表中重复命名的元素子集,类似于这里的其他答案

所以下次我查这个问题的时候我可以找到它

例如,从重复列表中子集b元素,其中每个元素包括a和b子元素:

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[[1]] > [1] 0.8815888 > > [[2]] > [1] 0.8407162
由v0.3.0于2019-11-06创建,请使用Lappy,目前此解决方案在mylist变大时根本无法扩展。我使用过此解决方案,但出于某种原因,它会在每个元素中添加一列包含索引。非常混乱。
x <- list(a=3, b="hello", c=4.5, d="world")
subsetList(x, c("d", "a"))
subsetList(x, c(4, 1))
[[1]]
[1] "world"

[[2]]
[1] 3