How can I delete N number of rows after N number of rows in R?

63 Views Asked by At

How can I keep the first 56 rows, then delete the next 6 rows, then keep the next 56 rows, then delete the next 6 rows repeatedly? I found some similar answers (like how to delete the Nth row), but not how to keep a pattern like mentioned.

My data is a data frame, 1 through 7910.

And I would like to Keep rows 1 through 56. Delete rows 57 through 62. Keep rows 63 through 118. Delete rows 119 through 124. And continue this pattern through all 7910 rows.

Thank you!

1

There are 1 best solutions below

0
s_baldur On

Using modulo (%%) of the row number:

df[((seq_len(nrow(df)) - 1L) %% 62L + 1L) <= 56L, ]

The same with dplyr:

df |> 
  filter(((seq_len(nrow(df)) - 1L) %% 62L + 1L) <= 56L)