这个问题可能已经很明显或已被提出,但我找不到解决方案:
我想创建一个包含所有可能组合(和变量数量)的数据框,使其类似于以下示例:
dataframe <- data.frame(variable = 1:4,
a = c("gender", NA, NA, NA),
b = c("age", NA, NA, NA),
c = c("city", NA, NA, NA),
d = c("education", NA, NA, NA),
e = c("gender", "age", NA, NA),
f = c("gender", "city", NA, NA),
g = c("gender", "education", NA, NA),
h = c("age", "city", NA, NA),
i = c("age", "education", NA, NA),
j = c("city", "education", NA, NA),
k = c("gender", "age", "city", NA),
l = c("gender", "age", "education", NA),
m = c("gender", "city", "education", NA),
n = c("gender", "age", "city", "education"))
我有太多变量,所以不值得写出来,我想避免错误。谢谢你的帮助!
答案 0 :(得分:3)
以下是combn
的选项。获取vector
变量名称,循环遍历vector
的序列,将combn
应用于vector
,并将m
指定为循环中的序列,将所有data.frame
元素一起转换为cbind
和list
。来自cbind.fill
的{{1}}适用于rowr
fill
NA
元素,其行数少于最大行list
data.frame
或者@Moody_Mudskipper建议,
library(rowr)
res <- do.call(cbind.fill, c(fill = NA, lapply(seq_along(v1), function(i) {
m1 <- combn(v1, i)
if(is.vector(m1)) as.data.frame.list(m1) else as.data.frame(m1)})))
colnames(res) <- letters[seq_along(res)]
res1 <- do.call(cbind.fill, c(fill = NA, lapply(seq_along(v1), function(i) combn(v1, i))))
colnames(res1) <- letters[seq_len(ncol(res1))]