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))
CodePudding user response:
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