Creating a new variable from a conditional operation on 3 old variables in R -
i have dataset in r, contains results of rapid diagnostic test. test has visible line if working (control line) , visible line each of 2 parasite species detects, if present in patient sample.
the dataset contains logical column each test line, follows: (database 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 add new column contains single result each rapid test. results designated according different logical conditions met 3 lines. example above new column 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 able create new column, takes lot of coding , think there has simpler (and shorter) way this.
here 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 simplify , shorten code appreciated, have kind of thing databases alot.
many thanks, amy
i create column of data frame , assign different subsets of conditionally. can slim down data frame indexing code.
rdtbase$result = na rdtbase <- within(rdtbase, result[control=="true" & pf=="true" & pv=="false"] <- "pf") rdtbase <- within(rdtbase, result[control=="false"] <- "invalid")
etc.
"within" saves little typing.
Comments
Post a Comment