【问题标题】:Combination of two lists with partial string matching (in R)两个列表与部分字符串匹配的组合(在 R 中)
【发布时间】:2021-08-04 07:36:44
【问题描述】:

我正在尝试查找两个列表的所有组合,但是第二个列表本质上是第一个列表变量的重复,并添加了括号等,如下所示。

other_cols <- c("C", "D", "E", "F")

other_colsRnd <- c("(1|C)", "(1|D)", "(1|E)", "(1|F)")

# I have some code to do combinations from one list:

combos = do.call(c, lapply(seq_along(other_cols), function(y) {
  arrangements::combinations(other_cols, y, layout = "l")
}))

theBigList = sapply(combos, paste, collapse = " + ")

> theBigList
 [1] "C"             "D"             "E"             "F"             "C + D"         "C + E"         "C + F"         "D + E"         "D + F"        
[10] "E + F"         "C + D + E"     "C + D + F"     "C + E + F"     "D + E + F"     "C + D + E + F"

我想要theBigList 中的完整组合列表,没有任何重复 C 和 (1|C)

########

编辑 C 或 D 等是“真实”变量的简写版本,看起来更像:

other_cols <- c("Charlie", "Delta", "Echo", "Foxtrot")
other_colsRnd <- c("(1|Charlie)", "(1|Delta)", "(1|Echo)", "(1|Foxtrot)")

########

预期的结果是这样的,尽管存储的顺序并不重要。

theBigList
"C"  "(1|C)"  "D"  "(1|D)"  "E"  "(1|E)"  "F"  "(1|F)"  "C + D"
"C + (1|D)"  "C + E"  "C + (1|E)"  "C + F"  "C + (1|F)"
"D + E"  "D + (1|E)"  "D + F"  "D + (1|F)"      
"E + F"  "E + (1|F)"
"C + D + E"  "(1|C) + D + E"  "(1|C) + (1|D) + E"  "(1|C) + (1|D) + (1|E)" etc.

有没有办法将lapply 放在lapply 中?

或者,我目前认为我可以comboRnd 例如

combosRnd = do.call(c, lapply(seq_along(other_cols), function(y) {
  arrangements::combinations(other_colsRnd, y, layout = "l")
}))

然后从here 中获取灵感,使用var_comb &lt;- expand.grid(combos, combosRnd) 以及某种if 和grep 来检测“相同”的变量,我还没有解决。

编辑

我想我认为,我可以添加组合,例如像

theBigList = sapply(combos, paste, collapse = " + ")
theBigListRnd = sapply(combosRnd, paste, collapse = " + ")
comboBigList = c(theBigList, theBigListRnd)
var_comb <- expand.grid(combos, combosRnd)
var_comb2 <- expand.grid(theBigList, theBigListRnd)

...所以comboBigList 包含所有没有交叉的地方,然后我可以删除任何一个或var_comb 或var_comb2 中与@ 中匹配的任何内容匹配的任何“行” 987654342@列。

是的,这是我之前提出的问题 here 中更简单的部分,但是我已经将其细化到完成这个地狱分析的必要性,因为我似乎可能已经咬得比我能咀嚼的还多。作为补充(希望如此),我会蛮力使用我需要的嵌套。

【问题讨论】:

    标签: r list combinations


    【解决方案1】:

    为什么不将other_cols 和other_colsRnd 结合使用,并使用与您相同的代码。

    combine_vec <- c(other_cols, other_colsRnd)
    
    combos <- do.call(c, lapply(seq_along(combine_vec), function(y) {
      arrangements::combinations(combine_vec, y, layout = "l")
    }))
    
    theBigList = sapply(combos, paste, collapse = " + ")
    theBigList
    
    #  [1] "C"                                            
    #  [2] "D"                                            
    #  [3] "E"                                            
    #  [4] "F"                                            
    #  [5] "(1|C)"                                        
    #  [6] "(1|D)"                                        
    #  [7] "(1|E)"                                        
    #  [8] "(1|F)"                                        
    #  [9] "C + D"                                        
    # [10] "C + E"                                        
    # [11] "C + F"                                        
    # [12] "C + (1|C)" 
    #...
    #...               
    

    从此theBigList,您可以使用以下代码删除variable + (1|variable) 组合。

    library(stringr)
    
    finalList <- theBigList[!mapply(function(x, y) any(x %in% y) || any(y %in% x), 
        str_extract_all(theBigList, '\\b[A-Z](?!\\))'), 
        str_extract_all(theBigList, '(?<=1\\|)[A-Z]'))]
    

    【讨论】:

    • 这适用于初始组合,但我不能在最终列表中包含像“C + (1|C)”这样的行。如果有一个像你的theBigList 的列表(或在var_comb 之前的expand.grid 想法),一些伪代码可能会说:循环并检查变量“C”的倍数。即If C &amp; (1|C) are in the same line delete it. 虽然它会循环通过other_cols 来检查所有要删除的内容。
    • 所以您不想在输出中使用variable + (1|variable) 的任何组合?所以也要删除像"(1|C) + (1|D) + D"这样的值?
    • 是的,没错。我最近的尝试还没有奏效,而且看起来也不优雅:newBigList &lt;- NULL for (l in combos3){ for (m in other_cols){ if (str_detect(m, paste("(1|", m, ")", sep = "")) == FALSE){ newBigList &lt;- c(combos3[l], newBigList) } } }
    • @MrSwaggins 查看我的更新答案。这可能会有所帮助。
    • 您可以根据您的实际数据更改正则表达式。因此,您可以使用\\w+ 而不是[A-Z] 来提取一个单词而不是一个字母。
    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    相关资源
    最近更新 更多