R:当列数为素数时,拆分数据框 [英] R: Split data frame when number of columns is a prime

查看:218
本文介绍了R:当列数为素数时,拆分数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 data.frame ,它有131列。我需要将其分为大约10到15个变量(即按列排序,而不是按行排列)。显然,由于131是素数,并不是所有的新数据帧的长度都可以相等。

I have a data.frame that has 131 columns. I need to part this into groups of about 10 to 15 variables (i.e., splitting by column, not by row!). Obviously, as 131 is a prime number, not all the new dataframes can be of equal length...

我在帖子中搜索了一个答案

I searched for an answer in the posts

  • How to cut data in even pieces in R?
  • Split a vector into chunks in R
  • Splitting a large vector into intervals in R

但是他们似乎都假设新的数据框架大小相同。

But they all seem to assume that the new data frames are of equal size.

编辑
感谢下面的意见,我会尝试澄清:

EDIT thanks to the comments below, I will try to clarify:

我的数据框看起来像这样

My data frame looks like this

head(trainData)
 ID          drop_vce_Range drop_dat_Range blck_vce_Range blck_dat_Range
48550           high            low           high            low
30965            low            low           high           high
40501            low            low            med            low
41771            med            low            low            low
42138            med            low            low            low
42975           high            low            low            low

此数据框有131列。我想在我的 globalenv 中有几个数据框,名称为Training_Part_1,Training_Part_2等等。这些新的数据框架中的每一个应该包含大约15列的旧数据框架。

This dataframe has 131 columns. I want to have several data frames in my globalenv, for instances with the names "Training_Part_1", "Training_Part_2" and so on. Each of these new data.frames should consist of about 15 columns of the old data.frame.

推荐答案

这将创建一个列表通过将内置数据框架的8列切割成3个不等的集合形成的数据框:

This creates a list of data frames formed by cutting the 8 columns of the built in data frame anscbome into 3 unequal sets:

k <- 3
nc <- ncol(anscombe)
lapply(split(as.list(anscombe), cut(1:nc, k, labels = FALSE)), as.data.frame)

这篇关于R:当列数为素数时,拆分数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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