问题
Having the following vector:
c("test1","test2","test3")
I am trying to get a list or data frame containing the following entries:
"test1" "test2" "test3"
"test1" "test2" NA
"test1" NA "test3"
"test1" NA NA
NA "test2" "test3"
NA "test2" NA
NA NA "test3"
The goal would be to get all possible subsets while the order doesn't matter, that is "text1" "text2" NA is equivalent to "text2" "text1" NA. I very much appreciate any help!
回答1:
You can use 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"
回答2:
There is a package sets with the relevant function.
library(sets)
a <- c("test1","test2","test3")
set_power(a)
{{}, {"test1"}, {"test2"}, {"test3"}, {"test1", "test2"}, {"test1", "test3"}, {"test2", "test3"}, {"test1", "test2", "test3"}}
This returns the set of all subsets.
回答3:
Using combn, and data.table::rbindlist with fill = TRUE option to make NA
values.
#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
来源:https://stackoverflow.com/questions/36197018/how-to-get-all-possible-subsets-of-a-character-vector-in-r