Data
I have a data.frame
that looks something like this:
df <- data.frame(id = c(1:10),
color = c(rep("red", 5), rep("blue", 5)))
df
#> id color
#> 1 1 red
#> 2 2 red
#> 3 3 red
#> 4 4 red
#> 5 5 red
#> 6 6 blue
#> 7 7 blue
#> 8 8 blue
#> 9 9 blue
#> 10 10 blue
Expected result
I'm trying to create a new column, say pair
that assigns a pair ID to each group of 2 consecutive IDs. For example, I want to end with a data.frame
that looks like:
df
#> id color pair
#> 1 1 red 1
#> 2 2 red 1
#> 3 3 red 2
#> 4 4 red 2
#> 5 5 red 3
#> 6 6 blue 3
#> 7 7 blue 4
#> 8 8 blue 4
#> 9 9 blue 5
#> 10 10 blue 5
Current method
All I'm wondering is whether there's a more concise way to achieve this, than what I'm already doing. I have looked through the seq()
documentation without any luck, though. Here is what I have currently, which gives me the desired output but is not very succinct.
df %>%
dplyr::mutate(pair = sort(rep(seq(length.out = nrow(df)/2),2)))
# id color pair
# 1 1 red 1
# 2 2 red 1
# 3 3 red 2
# 4 4 red 2
# 5 5 red 3
# 6 6 blue 3
# 7 7 blue 4
# 8 8 blue 4
# 9 9 blue 5
# 10 10 blue 5
Does anyone have any ideas, or another function besides seq()
that would do the job?