Membership Based Logic Vector

Thank you for your patience.

I am dealing with a large dataset that details patients and medications.

Medications are difficult to code because they are (usually) pointless if they are not consistent with the doses.

I have a dataframe with vectors (Drug1, Drug2 ..... Drug 16), where individual patients are represented in rows. Vectors are actually factors, with 100 levels of possible levels (all drugs that the patient may have).

All I want to do is create a logical vector (TTTTFFFFTTT ......), which I could then attach to a data frame that will tell me whether the patient is or not on a particular drug.

Then I could use the presence or absence of critical drugs as categorical covariances in the model.

I tried grep to search by strings and I can generate a vector of identifiers, but I can not generate a vector of logical elements.

I understand that I am doing something just wrong.

names(drugindex)
 [1] "book.MRN" "DRUG1"    "DRUG2"    "DRUG3"    "DRUG4"    "DRUG5"  
 [7] "DRUG6"    "DRUG7"    "DRUG8"    "DRUG9"    "DRUG10"   "DRUG11"  
[13] "DRUG12"   "DRUG13"   "DRUG14"   "DRUG15"   "DRUG16"  

> truvec<-drugindex$book.MRN[as.vector(unlist(apply(drugindex[,2:17], 2, grep, pattern="Lamotrigine")))]
> truvec
truvec
[1] 0024633  0008291  0008469  0030599  0027667
37 Levels: 0008291  0008469  0010188  0014217  0014439  0015822  ... 0034262

> 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         <NA>
5  0015822   Olanzapine  Venlafaxine        Lithium  Haloperidol   Alprazolam
         DRUG6      DRUG7      DRUG8      DRUG9          DRUG10 DRUG11 DRUG12
4  Lamotrigine Alprazolam    Lithium Alprazolam            <NA>   <NA>   <NA>
31 Lamotrigine   Ramipril Alprazolam   Zolpidem Trifluoperazine   <NA>   <NA>
3  Paracetamol Alprazolam Citalopram       <NA>            <NA>   <NA>   <NA>
13        <NA>       <NA>       <NA>       <NA>            <NA>   <NA>   <NA>
15        <NA>       <NA>       <NA>       <NA>            <NA>   <NA>   <NA>
5         <NA>       <NA>       <NA>       <NA>            <NA>   <NA>   <NA>
   DRUG13 DRUG14 DRUG15 DRUG16
4    <NA>   <NA>   <NA>   <NA>
31   <NA>   <NA>   <NA>   <NA>
3    <NA>   <NA>   <NA>   <NA>
13   <NA>   <NA>   <NA>   <NA>
15   <NA>   <NA>   <NA>   <NA>
5    <NA>   <NA>   <NA>   <NA>

And what I want is a vector of logic for each drug, saying whether this patient is with him

Thank you all for your time.

Ross Dunne MRCPsych

"Those who do not know, do not know, do not know."

+3
source share
2 answers

You were close to your attempt apply, but it MARGIN=2applies the function column by column, not row. Also grepreturns match locations; you want greplone that returns a logical vector. Try the following:

apply(x[,-1], 1, function(x) any(grepl("Aspirin",x)))

You can also use %in%which you can find more intuitive:

apply(x[,-1], 1, "%in%", x="Aspirin")
+9

-, . , "" , . , "" . , reshape. - :

library(reshape)
dnow <- melt(drugindex, id.var='book.MRN')
subset(dnow, value=='Lamotrigine')

, ...

: , cast:

cast(subset(dnow, value=='Lamotrigine'),  book.MRN ~ value)

@jonw .

+4

Source: https://habr.com/ru/post/1790807/


All Articles