Hi,
I have a dataset in R, which contains the results of a rapid diagnostic test. The test has a visible line if it is working properly (control line) and a visible line for each of the two parasite species it detects, if they are present in the patient sample.
The dataset contains a logical column for each test line, as follows: (database is called RDTbase)
Control Pf Pv
1. TRUE TRUE FALSE
2. TRUE FALSE TRUE
3. FALSE FALSE FALSE
4. TRUE TRUE TRUE
5. TRUE FALSE FALSE
I would like to add a new column which contains a single result for each rapid test. The results are designated according to the different logical conditions met by the three lines. For the example above the new column would look like this:
Control Pf Pv Result
1. TRUE TRUE FALSE Pf
2. TRUE FALSE TRUE Pv
3. FALSE FALSE FALSE Invalid
4. TRUE TRUE TRUE Mixed
5. TRUE FALSE FALSE Negative
I am able to create the new column, but it takes a lot of coding and I think there has to be a much simpler (and shorter) way to do this.
Here is my current (long) method:
R.Pf <- RDTbase[which(Control == "TRUE" & Pf == "TRUE" & Pv == "FALSE"),]
R.Pv <- RDTbase[which(Control == "TRUE" & Pf == "FALSE" & Pv == "TRUE"),]
R.inv <- RDTbase[which(Control == "FALSE"),]
R.mix <- RDTbase[which(Control == "TRUE" & Pf == "TRUE" & Pv == "TRUE"),]
R.neg <- RDTbase[which(Control == "TRUE" & Pf == "FALSE" & Pv == "FALSE"),]
R.Pf$Result <- c("Pf")
R.Pv$Result <- c("Pv")
R.inv$Result <- c("Invalid")
R.mix$Result <- c("Mixed")
R.neg$Result <- c("Negative")
RDTbase2 <- rbind(R.Pf, R.Pv, R.inv, R.mix, R.neg)
Any ideas on how to simplify and shorten this code would be greatly appreciated, as I have to do this kind of thing to my databases alot.
Many thanks, Amy