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.
Add a comment
|
1 Answer
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)
3 Comments
Nikolaos Bakogiannis
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 dataframesRonak Shah
See the updated answer to create new dataframes.
Nikolaos Bakogiannis
That was perfect! Thanks again for your help. It's highly appreciated!