【问题标题】:using list of predefined positions on list in r使用 r 中列表上的预定义位置列表
【发布时间】:2020-09-21 09:55:06
【问题描述】:

我有一个包含 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”上应用“索引”并提取值

这是我所做的一个可重复且简单的示例

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

只有一个元素有效,根据我的真实索引有近 5000 个数字

【问题讨论】:

    标签: r list subset


    【解决方案1】:

    我们可以使用sapply循环遍历'index'并一个一个提取元素(没有小例子,虽然不清楚)

    sapply(index, function(i) df[[2]][i][[8]][[1]][[1]])
    

    【讨论】:

    • 我用一个可重现且非常简单的示例编辑了我的帖子
    • @bloises 你需要sapply(index, function(i) lis[[1]][[i]][[1]])# [1] "a"。你的例子现在太简单了,所以不确定这是否代表你的确切情况
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多