如何在 R 中获取字符向量的所有可能子集?
How to get all possible subsets of a character vector in R?
具有以下向量:
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。我非常感谢任何帮助!
有相关功能的套餐。
library(sets)
a <- c("test1","test2","test3")
set_power(a)
{{}, {"test1"}, {"test2"}, {"test3"}, {"test1", "test2"}, {"test1", "test3"}, {"test2", "test3"},
{"test1", "test2", "test3"}}
这个returns所有子集的集合。
您可以使用 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"
使用 combn 和 data.table::rbindlist with 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
具有以下向量:
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。我非常感谢任何帮助!
有相关功能的套餐。
library(sets)
a <- c("test1","test2","test3")
set_power(a)
{{}, {"test1"}, {"test2"}, {"test3"}, {"test1", "test2"}, {"test1", "test3"}, {"test2", "test3"}, {"test1", "test2", "test3"}}
这个returns所有子集的集合。
您可以使用 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"
使用 combn 和 data.table::rbindlist with 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