Generally more efficient to filter before grouping. Note that summarize clears out whatever isn't mentioned in it, so the subsetting currently being done in the mean call could also be done in the pre-filter step and you can avoid filtering other columns and then discarding them by limiting the operations to only those columns that will be referenced:
tmp1 <- tmp %>% select( HCPCSCode, Avg_AllowByLimit, AllowByLimitFlag ) filter( AllowByLimitFlag & Avg_AllowByLimit != 0 ) %>% group_by( HCPCSCode ) %>% summarise( Avg_AllowByLimit = mean( Avg_AllowByLimit ) ) On May 22, 2019 6:45:39 AM PDT, Rui Barradas <ruipbarra...@sapo.pt> wrote: >Hello, > >Maybe filter the AllowByLimitFlag values first (not tested)? > > >tmp1 <- tmp %>% > group_by(HCPCSCode) %>% > filter(AllowByLimitFlag) %>% > summarise(Avg_AllowByLimit = >mean(Avg_AllowByLimit[which(Avg_AllowByLimit!=0)])) > > >Hope this helps, > >Rui Barradas > > >Às 13:35 de 22/05/19, Bill Poling escreveu: >> tmp1 <- tmp %>% >> group_by(HCPCSCode) %>% >> summarise(Avg_AllowByLimit = >mean(Avg_AllowByLimit[which(Avg_AllowByLimit!=0)])) >> >> # But I need Something like that + This >> >> WHERE AllowByLimitFlag == TRUE > >______________________________________________ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. -- Sent from my phone. Please excuse my brevity. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.