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

编程入门 行业动态 更新时间:2024-10-28 14:22:54
本文介绍了r:创建具有所有可能选项和变量组合数量的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个问题可能很明显,或者已经问过,但是我找不到解决方案:

This question might be obvious or asked already, but I can't find a solution:

我想创建一个包含所有可能组合(和数字)的数据框变量),使其看起来像下面的示例:

I want to create a data frame with all possible combinations (and number of variables) such that it looks like the following example:

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

我的变量太多,因此,不值得写出来,我想避免错误。谢谢您的帮助!

I have too many variables, so it's not worth writing it out, and I want to avoid errors. Thank you for helping!

推荐答案

以下是 combn 的选项。获取变量名称的 vector ,遍历 vector 的序列,应用 combn将向量上的与 m 指定为循环的序列,转换为 data.frame 和 cbind 所有 list 元素在一起。 rowr 中的 cbind.fill 适用于 fill 与 NA 用于 list 元素的行数少于最大行 data.frame

Here is an option with combn. Get the vector of variable names, loop through the sequence of the vector, apply the combn on the vector with m specified as the sequence from the loop, convert to data.frame and cbind all the list elements together. The cbind.fill from rowr is suitable to fill with NA for list elements that have less number of rows than the maximum row data.frame

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

或按照@Moody_Mudskipper的建议,

Or as @Moody_Mudskipper suggested,

res1 <- do.call(cbind.fill, c(fill = NA, lapply(seq_along(v1), function(i) combn(v1, i)))) colnames(res1) <- letters[seq_len(ncol(res1))]

数据

data

v1 <- c('gender', 'age', 'city', 'education')

更多推荐

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

本文发布于:2023-07-26 09:38:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   变量   选项   数量   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!