I used the following code to generate data.frame 'df' from my original data, 'pseudo'.
> df<-pseudo %>% group_by(Drug, CLSI_interpretation) %>%
summarise(n = n()) %>%
filter(Drug %in% c('Cefepime', 'Ceftazidime', 'Piperacillin','Piperacillin/tazobactam','Imipenem','Meropenem','Doripenem','Ciprofloxacin','Levofloxacin','Gentamicin','Tobramycin','Amikacin')) %>%
mutate(freq = (n/sum(n)*100))
Plus a very long mapvalues function that creates the 'class' column from 'Drug'.
All good so far; generates a dataset that looks like the following:
Drug CLSI n freq class
Amikacin I 7213 4.25503047 Aminoglycosides
Amikacin R 13995 8.25580915 Aminoglycosides
Amikacin S 148309 87.48916038 Aminoglycosides
Cefepime I 13326 8.87713502 Cephalosporins
Cefepime R 9744 6.49098031 Cephalosporins
Cefepime S 127046 84.63188468 Cephalosporins
Ceftazidime I 10836 5.98558290 Cephalosporins
Ceftazidime R 15276 8.43814732 Cephalosporins
Ceftazidime S 154923 85.57626978 Cephalosporins
Ciprofloxacin I 8949 4.74295103 Fluoroquinolones
Ciprofloxacin R 31563 16.72832309 Fluoroquinolones
I'm struggling with the next steps. I need to group this data by 'class', and for each class total the 'n' of CLSI %in% c('I','R') and generate a new frequency...basically, n(I + R)/n(I+R+S) and n(S)/n(I+R+S) for each class. Having a lot of trouble figuring out the summarise function because I need to summarise one variable (n) based on reference to another (CLSI), and keep grouped by a third (class). Thanks for your help.
lhs %>% rhs
, the%>%
operator is "Placing lhs as the first argument in rhs call". Thus, in your case, the result ofgroup_by(pseudo, class, CLSI)
is used as the.data
argument insummarize
. When you add "pseudo", it is no longer the first argument, but one of the...
.summarise
is expecting a single value of each call in...
. The 'result' of "pseudo" on the other hand is an entire data frame. Thus, the error. – Henrikdf %>% group_by(class) %>% mutate(prop.s = mean(CLSI == "S"), prop.ir = 1 - prop.s)
? – aosmithmutate(propn.s = sum(n[CLSI == "S"])/sum(n))
for each group? – aosmith