Note that there are some explanatory texts on larger screens.

plurals
  1. POVector of logicals based on row membership
    primarykey
    data
    text
    <p>thank you for your patience.</p> <p>I am dealing with a large dataset detailing patients and medications.</p> <p>Medications are hard to code, as they are (usually) meaningless unless matched with doses.</p> <p>I have a dataframe with vectors (Drug1, Drug2..... Drug 16) where individual patients are represented by rows. The vectors are actually factors, with 100s of possible levels (all the drugs the patient could be on).</p> <p>All I want to do is produce a vector of logicals (TTTTFFFFTTT......) that I could then cbind into a dataframe which will tell me whether a patient is or is not on a particular, drug.</p> <p>I could then use particularly important drugs' presence or absence as categorical covariates in a model.</p> <p>I've tried grep, to search along the rows, and I can generate a vector of identifiers, but I cannot seem to generate the vector of logicals.</p> <p>I realise I'm doing something simply wrong.</p> <pre><code>names(drugindex) [1] "book.MRN" "DRUG1" "DRUG2" "DRUG3" "DRUG4" "DRUG5" [7] "DRUG6" "DRUG7" "DRUG8" "DRUG9" "DRUG10" "DRUG11" [13] "DRUG12" "DRUG13" "DRUG14" "DRUG15" "DRUG16" &gt; truvec&lt;-drugindex$book.MRN[as.vector(unlist(apply(drugindex[,2:17], 2, grep, pattern="Lamotrigine")))] &gt; truvec truvec [1] 0024633 0008291 0008469 0030599 0027667 37 Levels: 0008291 0008469 0010188 0014217 0014439 0015822 ... 0034262 &gt; head(drugindex) book.MRN DRUG1 DRUG2 DRUG3 DRUG4 DRUG5 4 0008291 Venlafaxine Procyclidine Flunitrazepam Amisulpiride Clozapine 31 0008469 Venlafaxine Mirtazapine Lithium Olanzapine Metoprolol 3 0010188 Flurazepam Valproate Olanzapine Mirtazapine Esomeprazole 13 0014217 Aspirin Ramipril Zuclopenthixol Lorazepam Haloperidol 15 0014439 Zopiclone Diazepam Haloperidol Paracetamol &lt;NA&gt; 5 0015822 Olanzapine Venlafaxine Lithium Haloperidol Alprazolam DRUG6 DRUG7 DRUG8 DRUG9 DRUG10 DRUG11 DRUG12 4 Lamotrigine Alprazolam Lithium Alprazolam &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 31 Lamotrigine Ramipril Alprazolam Zolpidem Trifluoperazine &lt;NA&gt; &lt;NA&gt; 3 Paracetamol Alprazolam Citalopram &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 13 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 15 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 5 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; DRUG13 DRUG14 DRUG15 DRUG16 4 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 31 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 3 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 13 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 15 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 5 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; </code></pre> <p>And what I want is a vector of logicals for each drug, saying whether that patient is on it</p> <p>Thank you all for your time.</p> <p>Ross Dunne MRCPsych </p> <p>"Te occidere possunt sed te edere ne possunt, nefas est".</p>
    singulars
    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.
    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