Cam
Cam

Reputation: 453

Subset every 5 rows by group?

I have a dataset with multiple groups, and want to subset rows within groups along multiples of 5, with the addition of the first row (so row 1,5,10,15, etc within every group).

Right now my dataset has a column named "Group ID" and a few other columns (e.g. time, date, etc), but nothing indicating row numbers of any kind.

Any help would be appreciated! I was thinking maybe something compatible with dplyr? I was trying things using the function slice but no luck so far.

Upvotes: 0

Views: 399

Answers (1)

gss
gss

Reputation: 1453

You need to create the sequence within each group and then just use filter

library(dplyr)

df <- data.frame(id = c(1, 2, 1, 2, 2, 3, 4, 3, 1, 2, 4, 4, 4, 3, 1, 1, 1, 2, 2),
                 b = c(6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6))

df <- df %>% 
  group_by(id) %>% 
  mutate(group_index = row_number()) %>% 
  filter(group_index == 1 | group_index %% 5 == 0)

Upvotes: 2

Related Questions