【问题标题】:R: loop over list of lists to retrieve headers of sublists that contain a hitR:循环列表列表以检索包含命中的子列表的标题
【发布时间】:2020-11-28 06:11:48
【问题描述】:

我在 R 中有一个列表列表。列表列表中的每个子列表都包含多个元素。这些子列表不一定都具有相同的长度。所有子列表都有一个特定的标题名称。像这样:

#create list of lists
vector1 = c("apple","banana","cherry")
vector2 = c("banana","date","fig")
vector3 = c("fig","jackfruit","mango","plum")
listoflists  = list(vector1 , vector2, vector3)
names(listoflists) = c("listA", "listB", "listC")

列表列表如下所示:

listoflists

$listA
[1] "apple"  "banana" "cherry"

$listB
[1] "banana" "date"   "fig"   

$listC
[1] "fig"       "jackfruit" "mango"     "plum"     

接下来,我有一个向量,其中包含也可以在子列表中找到的元素。像这样:

wanted = c("apple","banana","fig")
wanted
[1] "apple"  "banana" "fig" 

对于向量中的每个元素 wanted 我想提取包含此特定元素的列表列表中每个子列表的标题名称。对于此处提供的示例,输出应如下所示:

#desired output
apple  listA
banana listA listB
fig    listB listC

我想过把它放到一个 for 循环中来获得这样的东西:

output_list = list()
for (i in wanted){
  output = EXTRACT LIST HEADER WHEN i IS PRESENT IN SUBLIST
  output_list[[i]] = output
}

但是,尚不清楚我是否可以,如果可以,如何循环遍历列表列表以仅提取那些包含向量 wanted 中元素的子列表的标题名称。我研究过使用unlist 函数,但这似乎对这个问题没有用。我查看了 stackoverflow 以及其他论坛,但找不到任何概述类似问题的问题。因此,如果有人能指出我解决这个问题的正确方向,那将非常有帮助。

已经谢谢了!

【问题讨论】:

    标签: r list loops vector sublist


    【解决方案1】:

    你可以使用stack + unstack

    unstack(subset(stack(listoflists), values%in%wanted), ind~values)
    
    $apple
    [1] "listA"
    
    $banana
    [1] "listA" "listB"
    
    $fig
    [1] "listB" "listC"
    

    【讨论】:

      【解决方案2】:

      这是另一个基本 R 选项

      u <- unlist(listoflists)
      sapply(wanted, function(x) rep(names(listoflists),lengths(listoflists))[u %in% x])
      

      给了

      $apple
      [1] "listA"
      
      $banana
      [1] "listA" "listB"
      
      $fig
      [1] "listB" "listC"
      

      【讨论】:

        【解决方案3】:

        有多种获取输出的方法。

        1) 一个选项是循环遍历“listoflists”,根据“想要”值将vector 子集化,stack 将其分为两列 data.frame 和 @987654324 @ 再次通过'values'变成list

        with(stack(lapply(listoflists, function(x) 
             x[x %in% wanted])), split(as.character(ind), values))
        #$apple
        #[1] "listA"
        
        #$banana
        #[1] "listA" "listB"
        
        #$fig
        #[1] "listB" "listC"
        

        2) 或者我们可以先stack 到两列'data.frame',然后subset 行,然后split

        with(subset(stack(listoflists), values %in% wanted), 
                   split(as.character(ind), values))
        #$apple
        #[1] "listA"
        
        #$banana
        #[1] "listA" "listB"
        
        #$fig
        #[1] "listB" "listC"
        

        3)) 或者另一种选择是遍历“wanted”并根据匹配获取“listoflists”的names

        setNames(lapply(wanted, function(x) 
           names(which(sapply(listoflists, function(y) x %in% y)))), wanted)
        #$apple
        #[1] "listA"
        
        #$banana
        #[1] "listA" "listB"
        
        #$fig
        #[1] "listB" "listC"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-10-30
          • 1970-01-01
          • 1970-01-01
          • 2021-09-15
          • 1970-01-01
          • 2017-01-13
          • 1970-01-01
          • 2019-03-07
          相关资源
          最近更新 更多