1

I have a wide dataset, I want to subset it by grouping every 50 variables and saving those as a separate dataframe to enable further analysis.

df1 <- df[,1:50]
df2 <- df[,51:100] etc

Is there a way of combining purrr and dplyr to loop over the entire data set to achieve this?

Alexan
  • 8,165
  • 14
  • 74
  • 101
scubasam
  • 21
  • 3
  • 1
    Possible duplicate of [Split up a dataframe by number of rows](https://stackoverflow.com/questions/7060272/split-up-a-dataframe-by-number-of-rows) – Paolo Jul 26 '17 at 19:10

1 Answers1

1

We can split by creating a grouping variable with %/% into a list of 'data.frame's.

lst <-  split.default(df, (seq_len(ncol(df))-1) %/%50 + 1)

It would be better to keep the datasets in the list instead of creating multiple objects in the global environment. But, if we need it, one option is list2env

list2env(setNames(lst, paste0("df", seq_along(lst))), envir = .GlobalEnv)

data

df <- as.data.frame(matrix(seq_len(1200), 10, 120))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    `ceiling(sequence(NROW(df))/50)` could also be used as grouping factor for `split` – d.b Jul 26 '17 at 19:12