Ultimately, I am looking to create a table that contains each unique ID I have in my dataset with a corresponding field, based on a "ranking" of that field.
I've been very stuck on this step. I have considered looping for each unique ID, but wanted to see if there was an easier way. Perhaps the apply family of functions could be of help. I'm also not sure of a way to rank different string values (e.g., TRUE > FALSE > NA).
Below is a small sample of what I'm looking at, with just the two fields of interest:
df1 <- data.frame(ID = c(1,1,2,2,3,3,3,4,4,5,6,7,7), flag = c("NA", "TRUE", "NA", "FALSE", "TRUE", "TRUE", "FALSE", "NA", "NA", "NA", "TRUE", "FALSE", "FALSE"))
For each ID:
- If there is at least one "TRUE" in the flag field, I want to pull one of those entire rows (doesn't matter which one).
- If an ID doesn't contain a "TRUE" value in the flag field, but has a "FALSE" in at least one row, I want to pull one of those entire rows.
- If an ID does not have a "TRUE" or "FALSE" value in the flag field, I still want one of the "NA" rows.
Below is the separate dataframe that I would ideally like to have:
ideal.df <- data.frame(ID = c(1,2,3,4,5,6,7), flag = c("TRUE", "FALSE", "TRUE", "NA", "NA", "TRUE", "FALSE"))
Thanks in advance for any help!