@EconProf has a good solution. Alternatively, you can identify MRN as the id_col in the pivot_wider function.
library(tidyr)
DF <- structure(list(
MRN = c("1", "1", "1", "1",
"1", "2", "3", "3", "3", "4"),
DOS = structure(c(1596758400, 1596758400, 1596758400, 1596758400,
1596758400, 1598572800, 1600732800, 1600732800, 1600732800, 1601337600), tzone = "UTC", class = c("POSIXct", "POSIXt")),
Drug = c("Succinylcholine", "Rocuronium", "Rocuronium", "Rocuronium", "Rocuronium", "Rocuronium",
"Rocuronium", "Rocuronium", "Rocuronium", "Rocuronium"),
Time = structure(c(1596786967.15, 1596787350.797, 1596790680, 1596794566.093, 1596811978.32, 1598601047.537,
1600764744.487, 1600780184.167, 1600788483.047, 1601365020), tzone = "UTC", class = c("POSIXct", "POSIXt")),
Dose = c(100, 20, 20, 20, 20, 100, 100, 50, 60, 50)),
row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
DF_wide <- DF |>
pivot_wider(id_cols = "MRN",names_from = "Drug", values_from = "Dose", values_fn = sum)
DF_wide
#> # A tibble: 4 × 3
#> MRN Succinylcholine Rocuronium
#> <chr> <dbl> <dbl>
#> 1 1 100 80
#> 2 2 NA 100
#> 3 3 NA 210
#> 4 4 NA 50
Created on 2022-09-20 with reprex v2.0.2