Splitting a data frame into equal parts

前端 未结 2 1022
情深已故
情深已故 2020-12-03 07:47

I have an example data frame:

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

Is there a way to print a list of data frames with the first

相关标签:
2条回答
  • 2020-12-03 08:34

    This can be solved with nesting using tidyr/dplyr

    require(dplyr) 
    require(tidyr)
    
    num_groups = 10
    
    iris %>% 
       group_by((row_number()-1) %/% (n()/num_groups)) %>%
       nest %>% pull(data)
    
    0 讨论(0)
  • 2020-12-03 08:49

    You could use split(), with rep() to create the groupings.

    n <- 10
    nr <- nrow(df)
    split(df, rep(1:ceiling(nr/n), each=n, length.out=nr))
    
    0 讨论(0)
提交回复
热议问题