2

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

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.

Community
  • 1
  • 1
PikkuKatja
  • 1,101
  • 3
  • 13
  • 21
  • 3
    `as.integer(131/15)` and `131 %% 15` for remainder. Obviously 131 can be `length()`. Can add loop to go through 10 to 15 and maximize remainder to make dataframes as equal as possible... – statespace Mar 12 '15 at 13:43
  • Can you provide a small reproducible example? What is your expected result? A list containing several data.frames? Multiple data.frames in your global environment? – talat Mar 12 '15 at 13:43

1 Answers1

2

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)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • this works fine. I was also able to get the parts of the list into my `globalenv` by using `list2env(x=by_Twenty, envir=.GlobalEnv ))`. Can you give me a short hint on how I can re-name the output data frames? I suppose I need to do something to `labels`? – PikkuKatja Mar 12 '15 at 14:01
  • 1
    Its normally better to keep them in a list and not put them directly into the global environment. If you want to add names then if `L` is the result of the `lapply` then, for example: `names(L) <- paste0("DF", seq_along(L))` – G. Grothendieck Mar 12 '15 at 14:10
  • that is exactly what I needed! And also, I will remember about not putting them in the global environment. It's something I'll have to read up on. – PikkuKatja Mar 12 '15 at 14:13