通过R中的3列将数据帧拆分为所有可能的数据帧组合 [英] Split a dataframe into all possible combinations of dataframes by 3 columns in R

查看:113
本文介绍了通过R中的3列将数据帧拆分为所有可能的数据帧组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从原始数据帧拆分为3列的所有可能组合中接收所有可能的数据帧。并且所有数据框必须包含id列。我处于死胡同,不知道如何保存所有可能的数据框,以便有可能进一步处理所有这些数据框。一种想法是将它们保存到列表中。但是我仍然不知道如何将所有必要的列绑定在一起。我发现一个问题,但还是有很大不同。此外,原始数据帧具有超过100万行和大约20列,因此使用data.table是合理的。

I need to receive all possible dataframes from the split of an original dataframe into all possible combinations of 3 columns. And all dataframes must contain id column. I'm at a dead end and do not know how to save all possible dataframes so that it will be possible to work further with all of them. One of the idea is to save them to list. But still I don’t know how to bind all necessary columns together. I find a close question to mine but it is still very different. Besides original dataframe has more than 1 mln rows and about 20 columns, so it is reasonable to use data.table.

frame <- data.frame(id = letters[seq( from = 1, to = 10 )], 
                    a = rnorm(10, 4), b = rnorm(10, 6), c=rnorm(10, 5),
                    d = rnorm(10, 2))

combos <- data.table(combn(colnames(frame[,-1]), 3))
combos <- data.table(t(rbind(combos, t(rep(colnames(output2[,1]), ncol(combos))))))
names(combos) <- c('category_1', 'category_2', 'category_3', 'id')

list_tables <- apply(combos, 1, as.list)

伙计们,我将不胜感激。预先感谢

Guys, I will appreciate any help. Thanks in advance

推荐答案

我建议将所有数据生成一个列表。只需生成一个列名称组合的矩阵(如您正在做的事情),然后一次使用它们即可:

I'd recommend not generating all the data into a list. Just generate a matrix of column name combinations (like what you're doing) and use them one-at-a-time:

combos = combn(colnames(frame[,-1]), 3)
combos = rbind("id", combos)

然后,您只需要使用 combos i 列作为子集框架

Then you just use the ith column of combos to subset frame on demand.

# first combo
frame[combos[, 1]]
# hundred and third combo
frame[combos[, 103]]
# etc.

最好将框架用作 data.table ,但是将 combos 用作矩阵将更简单,更有效。

It will be good to have frame be a data.table, but keeping combos as a matrix will be simpler and more efficient.

这篇关于通过R中的3列将数据帧拆分为所有可能的数据帧组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆