首页 文章

如何获得R中字符向量的所有可能子集?

提问于
浏览
2

有以下向量:

c("test1","test2","test3")

我想获取包含以下条目的列表或数据框:

"test1" "test2" "test3"
"test1" "test2" NA
"test1" NA "test3"
"test1"  NA NA
NA  "test2" "test3"
NA  "test2" NA
NA  NA "test3"

目标是获得所有可能的子集,而顺序无关紧要,即“text1”“text2”NA相当于“text2”“text1”NA . 我非常感谢任何帮助!

3 回答

  • 10

    你可以使用 combn

    res <- unlist(lapply(1:3, combn, 
                         x = c("test1","test2","test3"), simplify = FALSE), 
                  recursive = FALSE)
    res <- sapply(res, `length<-`, 3)
    #        [,1]    [,2]    [,3]    [,4]    [,5]    [,6]    [,7]   
    #[1,] "test1" "test2" "test3" "test1" "test1" "test2" "test1"
    #[2,] NA      NA      NA      "test2" "test3" "test3" "test2"
    #[3,] NA      NA      NA      NA      NA      NA      "test3"
    
  • 6

    有一套具有相关功能的套装 .

    library(sets)
    a <- c("test1","test2","test3")
    set_power(a)
    

    {{},{“test1”},{“test2”},{“test3”},{“test1”,“test2”},{“test1”,“test3”},{“test2”,“test3 “},{”test1“,”test2“,”test3“}}

    这将返回所有子集的集合 .

  • 6

    使用combn和data.table :: rbindlist和fill = TRUE选项来生成 NA 值 .

    #data
    a <- c("test1","test2","test3")
    
    #result
    data.table::rbindlist(
            sapply(1:3, function(i) as.data.frame(t(combn(a, i)))), fill = TRUE)
    
    #output
    #       V1    V2    V3
    # 1: test1    NA    NA
    # 2: test2    NA    NA
    # 3: test3    NA    NA
    # 4: test1 test2    NA
    # 5: test1 test3    NA
    # 6: test2 test3    NA
    # 7: test1 test2 test3
    

相关问题