Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fill in missing values in summarized dataframe in R

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading