使用列表中预定义的位置列表,在r

问题描述 投票:0回答:1

我有一个由10.000个元素组成的大而复杂的列表,我想只提取特定位置的值。所以,我已经做了一个对象,只有我想子集的位置。

   index
   [1]    1    3    5    9   18   23   24   25   26   32   37   52   56   57   58   59   60   64   70   72   74   75

我怎样才能只用这些数字来提取我想要的元素?

我试过这样的方法,但不成功。

df[[2]][index][[8]][[1]][[1]]

str(df)
.. .. .. ..$ DisorderMappingRelation        :List of 2
  .. .. .. .. ..$ Name  :List of 2
  .. .. .. .. .. ..$ text  : chr "BTNT (broader term maps to a narrower term)"
  .. .. .. .. .. ..$ .attrs: Named chr "en"
  .. .. .. .. .. .. ..- attr(*, "names")= chr "lang"
  .. .. .. .. ..$ .attrs: Named chr "21541"
  .. .. .. .. .. ..- attr(*, "names")= chr "id"
  .. .. .. ..$ DisorderMappingICDRelation     : NULL
  .. .. .. ..$ DisorderMappingValidationStatus:List of 2
  .. .. .. .. ..$ Name  :List of 2
  .. .. .. .. .. ..$ text  : chr "Validated"
  .. .. .. .. .. ..$ .attrs: Named chr "en"
  .. .. .. .. .. .. ..- attr(*, "names")= chr "lang"
  .. .. .. .. ..$ .attrs: Named chr "21611"
  .. .. .. .. .. ..- attr(*, "names")= chr "id"
  .. .. .. ..$ .attrs                         : Named chr "3669"
  .. .. .. .. ..- attr(*, "names")= chr "id"
  .. .. ..$ ExternalReference:List of 6
  .. .. .. ..$ Source                         : chr "OMIM"
  .. .. .. ..$ Reference                      : chr "219900"
  .. .. .. ..$ DisorderMappingRelation        :List of 2
  .. .. .. .. ..$ Name  :List of 2
  .. .. .. .. .. ..$ text  : chr "BTNT (broader term maps to a narrower term)"
  .. .. .. .. .. ..$ .attrs: Named chr "en"
  .. .. .. .. .. .. ..- attr(*, "names")= chr "lang"
  .. .. .. .. ..$ .attrs: Named chr "21541"
  .. .. .. .. .. ..- attr(*, "names")= chr "id"
  .. .. .. ..$ DisorderMappingICDRelation     : NULL
  .. .. .. ..$ DisorderMappingValidationStatus:List of 2
  .. .. .. .. ..$ Name  :List of 2
  .. .. .. .. .. ..$ text  : chr "Validated"
  .. .. .. .. .. ..$ .attrs: Named chr "en"
  .. .. .. .. .. .. ..- attr(*, "names")= chr "lang"
  .. .. .. .. ..$ .attrs: Named chr "21611"
  .. .. .. .. .. ..- attr(*, "names")= chr "id"
  .. .. .. ..$ .attrs                         : Named chr "11886"
  .. .. .. .. ..- attr(*, "names")= chr "id"
  .. .. ..$ .attrs           : Named chr "7"
  .. .. .. ..- attr(*, "names")= chr "count"
  .. ..$ DisorderDisorderAssociationList:List of 2
  .. .. ..$ text  : chr "\n      "
  .. .. ..$ .attrs: Named chr "0"
  .. .. .. ..- attr(*, "names")= chr "count"
  .. ..$ TextualInformationList         :List of 6
  .. .. ..$ TextualInformation:List of 2
  .. .. .. ..$ TextSectionList:List of 2
  .. .. .. .. ..$ TextSection:List of 3
  .. .. .. .. .. ..$ TextSectionType:List of 2
  .. .. .. .. .. .. ..$ Name  :List of 2
  .. .. .. .. .. .. .. ..$ text  : chr "Definition"
  .. .. .. .. .. .. .. ..$ .attrs: Named chr "en"
  .. .. .. .. .. .. .. .. ..- attr(*, "names")= chr "lang"
  .. .. .. .. .. .. ..$ .attrs: Named chr "16907"
  .. .. .. .. .. .. .. ..- attr(*, "names")= chr "id"
  .. .. .. .. .. ..$ Contents       : chr "A rare lysosomal disease characterized by an accumulation of cystine inside the lysosomes, causing damage in di"| __truncated__
  .. .. .. .. .. ..$ .attrs         : Named chr [1:2] "91672" "en"
  .. .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "id" "lang"
  .. .. .. .. ..$ .attrs     : Named chr "1"
  .. .. .. .. .. ..- attr(*, "names")= chr "count"
  .. .. .. ..$ .attrs         : Named chr [1:2] "1026" "en"
  .. .. .. .. ..- attr(*, "names")= chr [1:2] "id" "lang"
  .. .. ..$ TextualInformation:List of 2
  .. .. .. ..$ TextSectionList:List of 2
  .. .. .. .. ..$ text  : chr "\n          "
  .. .. .. .. ..$ .attrs: Named chr "0"
  .. .. .. .. .. ..- attr(*, "names")= chr "count"
  .. .. .. ..$ .attrs         : Named chr [1:2] "94635" "en"
  .. .. .. .. ..- attr(*, "names")= chr [1:2] "id" "lang"
  .. .. ..$ TextualInformation:List of 2
  .. .. .. ..$ TextSectionList:List of 2
  .. .. .. .. ..$ text  : chr "\n          "
  .. .. .. .. ..$ .attrs: Named chr "0"
  .. .. .. .. .. ..- attr(*, "names")= chr "count"
  .. .. .. ..$ .attrs         : Named chr [1:2] "74141" "en"
  .. .. .. .. ..- attr(*, "names")= chr [1:2] "id" "lang"
  .. .. ..$ TextualInformation:List of 2
  .. .. .. ..$ TextSectionList:List of 2
  .. .. .. .. ..$ text  : chr "\n          "
  .. .. .. .. ..$ .attrs: Named chr "0"
  .. .. .. .. .. ..- attr(*, "names")= chr "count"
  .. .. .. ..$ .attrs         : Named chr [1:2] "92580" "en"
  .. .. .. .. ..- attr(*, "names")= chr [1:2] "id" "lang"
  .. .. ..$ TextualInformation:List of 2
  .. .. .. ..$ TextSectionList:List of 2
  .. .. .. .. ..$ text  : chr "\n          "
  .. .. .. .. ..$ .attrs: Named chr "0"
  .. .. .. .. .. ..- attr(*, "names")= chr "count"
  .. .. .. ..$ .attrs         : Named chr [1:2] "115312" "en"
  .. .. .. .. ..- attr(*, "names")= chr [1:2] "id" "lang"
  .. .. ..$ .attrs            : Named chr "5"
  .. .. .. ..- attr(*, "names")= chr "count"
  .. ..$ .attrs                         : Named chr "11"
  .. .. ..- attr(*, "names")= chr "id"

有一些兴趣路径可以提取价值

df[[2]][[i]][[3]][[1]]  #Name
df[[2]][[i]][[1]]  #Class
df[[2]][[i]][[8]][[1]][[1]] #Reference 
df[[2]][[i]][[8]][[1]][[2]] #text

我想在 "i "上应用 "index",并提取数值。

这里是我做的一个可复制的简单例子

a = c("a", "b", "c")
b = c("b", "c", "a")
c = c("d", "a", "b")

lis = c(a,b,c)

df = data.frame(unlist(lapply(1:(length(lis[[1]])), function(i) lis[[1]][[i]][[1]])))
colnames(df) = "names"

index = which(df$names == "a")

lis[[1]][[index]][[1]]

只有一个元素,它的工作,我的实际指数有近5.000个数字。

r list subset
1个回答
1
投票

我们可以使用 sapply 在 "索引 "上循环,并逐一提取元素(如果没有一个小例子,就不清楚了)。

sapply(index, function(i) df[[2]][i][[8]][[1]][[1]])
© www.soinside.com 2019 - 2024. All rights reserved.