Varun Varun - 23 days ago 6
R Question

Split dataframe into equal parts and store each part as a separate data frame

I have a large dataframe, I'd like to split this into multiple smaller data frames of equal parts.

A sample df:

df <- data.frame(x = 1:100, y = runif(100))


I have the code that splits them into equal parts (let's say 10 dataframes of 10 rows each)

x=split(df, (seq(nrow(df))-1) %/% 10)


and stores them in a list
x
, but I can't seem to figure out how to convert each part of
x
to a separate dataframe.

I tried to use
lapply
but my method didn't work out the way I wanted to

Any ideas?

Answer Source

You want list2env:

list2env(setNames(x,paste0("df",1:10)),environment())
# df3
# x         y
# 21 21 0.4935413
# 22 22 0.1862176
# 23 23 0.8273733
# 24 24 0.6684667
# 25 25 0.7942399
# 26 26 0.1079436
# 27 27 0.7237109
# 28 28 0.4112744
# 29 29 0.8209463
# 30 30 0.6470602