I'm currently tidying a dataset as part of a clinical project I am working on. I am trying to extract the best of the measurements for each parameter (e.g. FEV1/FVC/FEF) per patient when there are duplicate dates on which their measurements were taken. I'll attach my reprex at the end to explain further.
Basically I want to make a new variable for each parameter with the best of measurements, then combine them together with the other variables to make a new, refined dataframe in order to have it matched in size to another dataframe I am using.
Was wondering if anyone would be able to help me with this, as so the only way I've found is to do it manually. This works but I'd rather not do that due to the risk of human error and it being quite time consuming (I intend to use my script on multiple other sites in this project that could have larger data and thus take more time to refine).
Here is the reprex. Thanks for any help that can be provided.
#Test data frame with some extracted values
#This is just a sample of one patient, but it shows the duplicate dates I am referring to
data <- data.frame(PATIENT_ID = c(-2147483646, -2147483646, -2147483646, -2147483646, -2147483646, -2147483646, -2147483646, -2147483646, -2147483646, -2147483646),
DATE = c("2018-08-06", "2018-08-20", "2018-08-20", "2018-08-20", "2018-10-01", "2018-10-01", "2018-10-01", "2019-02-04", "2019-02-04", "2019-02-04"),
FEV1_L = c(3.974, 4.024, 4.053, 4.046, 4.241, 4.213, 3.944, 4.027, 3.984, 4.247),
FVC_L = c(5.198, 5.250, 5.043, 5.286, 5.305, 5.284, 4.922, 5.255, 5.426, 5.526),
FEF_LM = c(199.2, 196.8, 226.9, 199.3, 249.0, 237.7, 225.2, 223.8, 205.1, 226.9))
#Manual attempt to extract the best measurements and make them into separate variables
data.fev1 <- data[c(3, 5, 10),]
data.fvc <- data[c(4, 5, 10),]
data.fef <- data[c(3, 5, 10),]
#Combining manually extracted best of together with rest of data frame to make a new refined dataframe
data.new <- dplyr::bind_rows(data.fev1, data.fvc)
data.final <- dplyr::bind_rows(data.new, data.fef)
#Alternative that results in the same but isn't done manually?