Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a new variable from a conditional operation on 3 old variables in R
    primarykey
    data
    text
    <p>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.</p> <p>The dataset contains a logical column for each test line, as follows: (database is called RDTbase)</p> <pre><code> Control Pf Pv 1. TRUE TRUE FALSE 2. TRUE FALSE TRUE 3. FALSE FALSE FALSE 4. TRUE TRUE TRUE 5. TRUE FALSE FALSE </code></pre> <p>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:</p> <pre><code>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 </code></pre> <p>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.</p> <p>Here is my current (long) method:</p> <pre><code>R.Pf &lt;- RDTbase[which(Control == "TRUE" &amp; Pf == "TRUE" &amp; Pv == "FALSE"),] R.Pv &lt;- RDTbase[which(Control == "TRUE" &amp; Pf == "FALSE" &amp; Pv == "TRUE"),] R.inv &lt;- RDTbase[which(Control == "FALSE"),] R.mix &lt;- RDTbase[which(Control == "TRUE" &amp; Pf == "TRUE" &amp; Pv == "TRUE"),] R.neg &lt;- RDTbase[which(Control == "TRUE" &amp; Pf == "FALSE" &amp; Pv == "FALSE"),] R.Pf$Result &lt;- c("Pf") R.Pv$Result &lt;- c("Pv") R.inv$Result &lt;- c("Invalid") R.mix$Result &lt;- c("Mixed") R.neg$Result &lt;- c("Negative") RDTbase2 &lt;- rbind(R.Pf, R.Pv, R.inv, R.mix, R.neg) </code></pre> <p>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.</p> <p>Many thanks, Amy</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload