Message-ID: <A7D83169-C0AC-446D-9417-A44337677896@comcast.net>
Date: 2015-08-31T23:48:15Z
From: David Winsemius
Subject: Conditional replacement and removal of data frame values
In-Reply-To: <CAMk+s2R_yPO3ygNFLUnaM9TVZy7ZB7neO-3o_=4s2jRGACHZqg@mail.gmail.com>
On Aug 31, 2015, at 1:49 PM, Luigi Marongiu wrote:
> 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.