set.seed(1)
df <-data.frame(category=rep(LETTERS[1:5],each=10),superregion=sample(c("EMEA","LATAM","AMER","APAC"),100,replace=T),country=sample(c("Country1","Country2","Country3","Country4","Country5","Country6","Country7","Country8"),100,replace=T),market=sample(c("Market1","Market2","Market3","Market4","Market5","Market6","Market7","Market8","Market9","Market10","Market11","Market12"),100,replace=T),hospitalID=sample(seq(1,50,1),100,replace=T),IndicatorFlag=sample(seq(0,1,1),100,replace=T))
I'm trying to create a SummaryTab grouped by level and a binaryIndicator, where level is an argument that could be country or market. I hence want to combine the following examples into one, by making level an argument.
SummaryTab1 = ddply(df, .(market,IndicatorFlag), summarize, counts=length(unique(hospitalID)))
SummaryTab1 = ddply(df, .(country,IndicatorFlag), summarize, counts=length(unique(hospitalID)))
With level as an argument, I tried the following:
level<-c("market")
string<-paste(level,"IndicatorFlag",sep=" , ")
SummaryTab1 = ddply(df,.(string) , summarize, counts=length(unique(hospitalID)))
which just gives a string
I also tried this
SummaryTab1 =as.formula(paste0("ddply(df,.(",level,",IndicatorFlag),summarize, counts=length(unique(hospitalID)))"))
Any suggestions how to do this ?
What I'm trying to do is group by the level and the IndicatorFlag
In terms of what Miha suggested, I'm trying to do this (Neither works):
library(dplyr)
SumTab<-df %>% group_by_(my_level,IndicatorFlag) %>% summarise(counts = length(unique(hospitalID)))
SumTab<-ddply(df, .(my_level[2],IndicatorFlag), summarize, counts=length(unique(hospitalID)))