scubasam
scubasam

Reputation: 21

Subsetting using dplyr

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?

Upvotes: 1

Views: 99

Answers (1)

akrun
akrun

Reputation: 886938

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))

Upvotes: 1

Related Questions