问题
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
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.
回答1:
This creates a list of data frames formed by cutting the 8 columns of the built-in data frame anscombe
into 3 unequal sets:
k <- 3
nc <- ncol(anscombe)
lapply(split(as.list(anscombe), cut(1:nc, k, labels = FALSE)), as.data.frame)
来源:https://stackoverflow.com/questions/29011315/r-split-data-frame-when-number-of-columns-is-a-prime