data current have
id<-c(234, 234, 256, 256, 264, 264, 290, 290)
event_date<-c("2023-11-01", "2023-11-01", "2023-11-01", "2023-11-01", "2023-11-03", "2023-11-03","2023-11-03","2023-11-03")
bp_codes <- c("001", "002", "001", "002", "001", "002", "001", "002")
code_desc <- c("systolic_bp", "diastolic_bp", "systolic_bp", "diastolic_bp", "systolic_bp", "diastolic_bp", "systolic_bp", "diastolic_bp")
BP_values1 <- c(170, "NA", 135, "NA", 150, "NA", 145, "NA")
BP_Values2 <- c("NA", "100", "NA", "85", "NA", "95", "NA", "80")
bp_data <- data.frame(id, event_date,bp_codes,code_desc,BP_values1, BP_Values2)
rm(bp_data2)
desired results is to combine roles with same ID and identical event_date creating 2 new columns sbp (BP_values labelled systolic_bp) and dbp (BP_values labelled diastolic_bp)
id2<-c(234, 256, 264, 290)
event_date2<-c("2023-11-01", "2023-11-01", "2023-11-03","2023-11-03")
bp_codes2 <- c("001-002", "001-002", "001-002", "001-002")
code_desc2 <- c("bp_reading", "bp_reading", "bp_reading","bp_reading")
sbp <- c(170, 135, 150, 145)
dbp <- c("100", "85", "95", "80")
bp_data2 <- data.frame(id2, event_date2,bp_codes2,code_desc2,sbp,dbp)