I have this tibble with 8 groups:
df <- structure(list(group1 = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6,
6, 7, 8, 8)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-16L))
group1
<dbl>
1 1
2 1
3 2
4 2
5 3
6 3
7 4
8 4
9 5
10 5
11 5
12 6
13 6
14 7
15 8
16 8
How can I create a new group2 that combines each consecutive N (let's say N=2) groups to one new group like:
group1 group2
<dbl> <dbl>
1 1 1
2 1 1
3 2 1
4 2 1
5 3 2
6 3 2
7 4 2
8 4 2
9 5 3
10 5 3
11 5 3
12 6 3
13 6 3
14 7 4
15 8 4
16 8 4
Background: If I want to do it with rows I would use
df %>%
mutate(Col2 = rep(row_number(), each=2, length.out = n()))
But instead of row_number()
I would like to say group
.