I have a sorted, summarized (unsure of the correct terminology) dataframe where rows/ranks with triplicated count values have been removed, leaving just the first and last instance of that count value. I would like to regenerate the intervening rows. Here is some toy data:
df_have <- data.frame(rank = c(1,2,3,5,6,7,9,10), count = c(10,9,8,8,7,5,5,3))
df_have
rank count
1 1 10
2 2 9
3 3 8
4 5 8
5 6 7
6 7 5
7 9 5
8 10 3
df_want <- data.frame(rank = c(1:10), count = c(10,9,8,8,8,7,5,5,5,3))
df_want
rank count
1 1 10
2 2 9
3 3 8
4 4 8
5 5 8
6 6 7
7 7 5
8 8 5
9 9 5
10 10 3
Is there a simple way to do this? Thanks!
>Solution :
library(tidyr)
df_have |>
complete(rank = seq(min(rank), max(rank))) |>
fill(count)
rank count
<dbl> <dbl>
1 1 10
2 2 9
3 3 8
4 4 8
5 5 8
6 6 7
7 7 5
8 8 5
9 9 5
10 10 3