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

Insert NA after specified number of rows using R

I got a df such as this

structure(list(id = c(1, 1, 1, 2, 2, 2), hosp = c(156, 186, 891, 
221, 231, 246), out = c(185, 235, 1062, 230, NA, 250), insti = c(NA, 
NA, NA, NA, 245, NA), days = c(1063, 1063, 1063, 251, 251, 251
), status = c(1, 1, 1, 1, 1, 1)), class = "data.frame", row.names = c(NA, 
-6L))

and I would like to keep the values in the last row of column "days" and "status" for each id. I want the data to look like this

structure(list(id2 = c(1, 1, 1, 2, 2, 2), hosp2 = c(156, 186, 
891, 221, 231, 246), out2 = c(185, 235, 1062, 230, NA, 250), 
    insti2 = c(NA, NA, NA, NA, 245, NA), days2 = c(NA, NA, 1063, 
    NA, NA, 251), status2 = c(NA, NA, 1, NA, NA, 1)), class = "data.frame", row.names = c(NA, 
-6L))

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 :

A possible solution:

library(tidyverse)

df %>% 
  group_by(id) %>% 
  mutate(days = if_else(row_number() == n(), days, NA_real_),
         status =  if_else(row_number() == n(), status, NA_real_)) %>% ungroup

#> # A tibble: 6 × 6
#>      id  hosp   out insti  days status
#>   <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
#> 1     1   156   185    NA    NA     NA
#> 2     1   186   235    NA    NA     NA
#> 3     1   891  1062    NA  1063      1
#> 4     2   221   230    NA    NA     NA
#> 5     2   231    NA   245    NA     NA
#> 6     2   246   250    NA   251      1
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