I have long form patient prescription data and want to create a wider data frame where each line represents a different prescription delivery. So some patients will have only one row, but those with multiple deliveries will have several rows (1 for each prescription delivery). I have only previously used the pivot commands in a very simple manner, but am struggling as I am only getting back 1 row for each patient, when I want 1 row for each date of prescription delivery for each patient.
I have a very simple data frame of patient id, date of prescription delivery and the code corresponding to the prescription they recieved.
id = id = factor(c("1001","1001","1001","1002","1002","1002","1002","1002","1003","1003"))
date = c("2013-10-31","2013-11-30","2013-12-31","2013-08-28","2013-08-28","2013-09-30",
"2013-09-30","2013-02-15","2013-02-15","2013-02-15")
atc_code = c("C07AA05","C07AA05","C07AA05","A10BA02","C09CA01","A10BA02",
"C09CA01","A10BA02","A10BA02","C07AA05")
date1 <- as.Date(date, format = "%Y-%m-%d")
df <- data.frame(id,
date1,
atc_code)
df
#> id date1 atc_code
#> 1 1001 2013-10-31 C07AA05
#> 2 1001 2013-11-30 C07AA05
#> 3 1001 2013-12-31 C07AA05
#> 4 1002 2013-08-28 A10BA02
#> 5 1002 2013-08-28 C09CA01
#> 6 1002 2013-09-30 A10BA02
#> 7 1002 2013-09-30 C09CA01
#> 8 1002 2013-02-15 A10BA02
#> 9 1003 2013-02-15 A10BA02
#> 10 1003 2013-02-15 C07AA05
Created on 2021-12-04 by the reprex package (v2.0.1)
What I would like the data frame to look like:
df
#> id date atc_code_1 atc_code_2
#> 1 1001 2013-10-31 C07AA05 NA
#> 2 1001 2013-11-30 C07AA05 NA
#> 3 1001 2013-12-31 C07AA05 NA
#> 4 1002 2013-08-28 A10BA02 C09CA01
#> 5 1002 2013-09-30 A10BA02 C09CA01
#> 6 1002 2013-02-15 A10BA02 NA
#> 7 1003 2013-02-15 A10BA02 C07AA05
In reality, a patient can have many more deliveries in the year and many more prescriptions in a single delivery, but I kept it simple for the example. Any help would be greatly appreciated.
What I need to do is create a new variable with mutate (a disease) that uses combinations of prescription in a single delivery to define (ie did a patient get x and y prescription or did they get x but not y prescription), so if this can be achieved by a series of group_bys or something else, that would work too.
Thank you!