[R] Conditional replacement and removal of data frame values

Luigi Marongiu marongiu.luigi at gmail.com
Mon Aug 31 22:49:15 CEST 2015


Dear all,
I have a data frame and I would like to do the following:
a) replace value of one variable "a" according to the value of another one "b"
b) remove all the instances of the variable "b"

For the sake of argument, let's say I have the following data frame:
test <- rep(c("Adenovirus", "Rotavirus", "Norovirus", "Rotarix",
"Sapovirus"), 3)
res <- c(0, 1, 0, 0, 1,
         1, 0, 1, 1, 0,
         0, 1, 0, 1, 0)
samp <- c(rep(1, 5), rep(2, 5), rep(3, 5))
df <- data.frame(test, res, samp, stringsAsFactors = FALSE)

The task I need is to coerce the results of the "Rotavirus" to
negative (0) if and only if "Rotarix" is positive (1). In this
example, the results shows that for "samp" 3 "Rotavirus" should be 0:
    test           res samp
2  Rotavirus   1    1
4  Rotarix       0    1
7  Rotavirus    0    2
9  Rotarix       1    2
12 Rotavirus   1    3
14 Rotarix       1    3

I can't use the subset function because then I would work on a
separate object and I don't know how to implement the conditions for
the replacements.
Finally, all the "Rotarix" entries should be removed from the data frame.
Thank you.
Best regards,
Luigi



More information about the R-help mailing list