拥有以下载体:
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。我非常感谢任何帮助!
答案 0 :(得分: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"
答案 1 :(得分:6)
有一套具有相关功能的套装。
library(sets)
a <- c("test1","test2","test3")
set_power(a)
{{},{“test1”},{“test2”},{“test3”},{“test1”,“test2”},{“test1”,“test3”},{“test2”, “TEST3”}, {“test1”,“test2”,“test3”}}
返回所有子集的集合。
答案 2 :(得分: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