0

My issue is that after reading a Json file and then using tibble::enframe(unlist(data)), I want to create multiple dataframes by splitting the existing one at 20th*n, where (n=1,2..nrow(data)/20) rows. By simply subsetting it, I am getting what I want with data[1:20,]. I am also aware of the split() and slice() functions. I am wondering how can I come up with multiple dataframes by looping over the initial dataframe. Thnx in advance for any suggestion.

1 Answer 1

1

You can use split creating an index of every n rows to split the data.

n <- 20
df <- data.frame(a = runif(100), b = rnorm(100))
list_df <- split(df, ceiling(seq(nrow(df))/n))

To create new dataframes -

names(list_df) <- paste0('df', seq_along(list_df))
list2env(list_df, .GlobalEnv)
Sign up to request clarification or add additional context in comments.

3 Comments

Hallo Ronak and thanks a lot for your answer. This main issue I have again is that I want to come up with a solution that returns new dataframes. What you proposed to me creates Large list, from which I can retrieve every element and then transform it to a dataframe --> as.data.frame(list_df [1]). What I want though is to loop through that large list and create the respective dataframes
See the updated answer to create new dataframes.
That was perfect! Thanks again for your help. It's highly appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.