0

I need to split a dataframe every 30 rows to build a subset of it. Syntax would look like this, but I really need more clever way, since I have got 9600 rows.

x1 <- mtcars[1:3, ]
x2 <- mtcars[4:6, ]
x3 <- mtcars[7:9, ]
x4 <- mtcars[10:12, ]

Each subsets of mtcars has 3 rows. Is there a way to do this? A sample is not an option, every 30 rows contain a block of information which I have to summarise later.

Thank you!

Data Mastery
  • 1,555
  • 4
  • 18
  • 60
  • Are you sure you want to `split` the data or do you want to group the data? – akash87 Jan 24 '20 at 15:59
  • split is right, yes. I can not group because only the first row contains information about the brand, the other 29 rows are NA – Data Mastery Jan 24 '20 at 15:59
  • In `tidyverse` I would consider something like `mtcars %>% mutate(grp = cut(row_number(), seq(0,dim(mtcars)[1], by = 3))) %>% group_by(grp) %>% top_n(1)` – akash87 Jan 24 '20 at 16:04

0 Answers0