-4

I wonder if there is an easy way to split an R dataset / data.frame into e.g. 10 groups of the same size? I have a dataset which is ordered by a response variable. I want to analyze the avaerage response rates for 10 groups, descending in response value.

Thank you a lot!

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • 1
    Try `split()` in R. – tushaR Oct 20 '19 at 19:17
  • `split(iris, sample(rep(seq(10), nrow(iris) / 10)))` or more usually, `split(iris, iris$Species)` – alistaire Oct 20 '19 at 19:20
  • hi @alistaire, this works just fine! Thank you! – me_overfolwn Oct 20 '19 at 19:42
  • Related: [1](https://stats.stackexchange.com/questions/149250/split-data-into-n-equal-groups), [2](https://stackoverflow.com/questions/41701064/r-split-dataframe-into-3-parts), [3](https://stackoverflow.com/questions/18139708/split-data-frame-into-rows-of-fixed-size). – Rui Barradas Oct 20 '19 at 20:03

1 Answers1

0

An option would be using the tidyverse. The function group_by() lets you group your dataframe by one or more variables and then use the summarise() function to perform specific task on each group.

An example:

library(tidyverse)
Result <- mtcars %>% group_by(cyl) %>% 
 summarise(MeanMPG = mean(mpg)) %>% arrange(MeanMPG)
Result
Orlando Sabogal
  • 1,470
  • 7
  • 20