Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to count occurrences of several strings per row in a data frame in R

$
0
0

i have a question for which I thought I found a solution but if I double checked by hand I got numbers. I searched in other quotes but couldn't get exactly what I am looking for.

I have a dataframe with pharmaceutical agents. Each row is a subject and up to 20 columns store an agent each.Then I have a list of agents that can be clustered for one purpose. E.g. beta blockers. What I would like to do is iterate over each row to count if and how many e.g. beta blockers or statins a subject is taking.

I have tried with:

BETA = c("METOPROLOL", "BISOPROLOL", "NEBILET", "METOHEXAL", "SOTALEX",
         "QUERTO", "NEBIVOLOL", "CARVEDILOL", "METOPROLOLSUCCINAT", "BELOC")
for (i in 1:202) {
  dat$betablock[i] <- sum(str_count(meds[i,], BETA ))
} 

I don't get a warning but it doesn't count the correct number of occurrences.

Here is some sample data:

       Med1         Med2       Med3          Med4            Med5          Med6       Med7         Med8        Med9     Med10      Med11    Med12    Med13    Med14      Med15
1  AMLODIPIN     RAMIPRIL METOPROLOL                                                                                                                                           
2     PLAVIX  SIMVASTATIN MIRTAZAPIN                                                                                                                                           
3 BISOPROLOL    AMLODIPIN        ASS     VALSARTAN    CHLORALDURAT     Doxozosin TAMSULOSIN     CIPRAMIL                                                                       
4        ASS    ENALAPRIL L-THYROXIN       LITALIR         LITALIR     AMLODIPIN  CETIRIZIN          HCT        NACL    CARMEN PROTEIN 88 NOVALGIN                             
5        ASS ATORVASTATIN    FOSAMAX       CALCIUM        PANTOZOL NOVAMINSULFON                                                                                               
6        ASS      FRAGMIN  TORASEMID SPIRONOLACTON LORZAAR PROTECT       VESIKUR  ROCALTROL ATORVASTATIN PREDNISOLON LACTULOSE MIRTAZAPIN   LANTUS ACTRAPID PANTOZOL SALBUTAMOL
          Med16 Med17 Med18 Med19 Med20
1                                      
2                                      
3                                      
4                                      
5                                      
6 AMPHO MORONAL                        

As you can see in the first row third column the string 'METOPROLOL' is listed. But when I call the result of my for loop for the first subject it results '0'.

> dat$betablock[1]
[1] 0

Any suggestions?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>