r:创建包含所有可能选项和变量组合数的数据框

时间:2017-07-18 08:53:37

标签: r variables dataframe combinations

这个问题可能已经很明显或已被提出,但我找不到解决方案:

我想创建一个包含所有可能组合(和变量数量)的数据框,使其类似于以下示例:

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"))

我有太多变量,所以不值得写出来,我想避免错误。谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

以下是combn的选项。获取vector变量名称,循环遍历vector的序列,将combn应用于vector,并将m指定为循环中的序列,将所有data.frame元素一起转换为cbindlist。来自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))]