Good morning. #R version 3.6.0 Patched (2019-05-19 r76539) #Platform: x86_64-w64-mingw32/x64 (64-bit) #Running under: Windows >= 8 x64 (build 9200) I need a calculated field For the Rate of Avg_AllowByLimit where the Allowed_AmtFlag = TRUE BY Each Code I have almost got this. #So far I have this 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 I cannot seem to get it in there correctly Thank you for any help WHP #Here is some data HCPCSCode Avg_AllowByLimit AllowByLimitFlag 1 J1745 4.50 FALSE 2 J9299 18.70 FALSE 3 J9306 14.33 FALSE 4 J9355 7.13 FALSE 5 J0897 8.61 FALSE 6 J9034 3.32 FALSE 7 J9034 3.32 FALSE 8 J9045 15.60 FALSE 9 J9035 2.77 TRUE 10 J1190 3.62 FALSE 11 J2250 879.10 FALSE 12 J9033 2.92 FALSE 13 J1745 4.50 TRUE 14 J2785 12.11 FALSE 15 J9045 15.60 FALSE 16 J2350 7.81 FALSE 17 J2469 10.65 TRUE 18 J2796 6.27 FALSE 19 J2796 6.27 FALSE 20 J9355 7.13 FALSE 21 J9045 15.60 FALSE 22 J2505 2.73 FALSE 23 J1786 2.81 FALSE 24 J3262 3.26 FALSE 25 J0696 168.87 FALSE 26 J0178 1.52 TRUE 27 J9271 5.55 FALSE 28 J3380 80.99 FALSE 29 J9355 7.13 TRUE 30 J2469 10.65 FALSE 31 J9045 15.60 FALSE 32 J1459 3.64 FALSE 33 J9305 8.74 FALSE 34 J9034 3.32 FALSE 35 J9034 3.32 FALSE Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}}
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
Thank you Rui, but that is getting me the overall mean not the mean of just the observations that are flagged as TRUE Grouped By the HCPCSCode (AllowByLimitFlag == TRUE). I also tried adding it to the filter you suggested but that does not seem to work either? tmp1 <- tmp %>% group_by(HCPCSCode) %>% filter(AllowByLimitFlag==TRUE) %>% summarise(Avg_AllowByLimit mean(Avg_AllowByLimit[which(Avg_AllowByLimit!=0)])) However, I really appreciate your help Sir! WHP William H. Poling, Ph.D., MPH | Manager, Data Science Data Intelligence & Analytics Zelis Healthcare -----Original Message----- From: Rui Barradas <ruipbarradas at sapo.pt> Sent: Wednesday, May 22, 2019 9:46 AM To: Bill Poling <Bill.Poling at zelis.com>; r-help (r-help at r-project.org) <r-help at r-project.org> Subject: Re: [R] Help with R coding 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 == TRUEConfidentiality Notice This message is sent from Zelis. This transmission may contain information which is privileged and confidential and is intended for the personal and confidential use of the named recipient only. Such information may be protected by applicable State and Federal laws from this disclosure or unauthorized use. If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, review, discussion, copying, or taking any action in reliance on the contents of this transmission is strictly prohibited. If you have received this transmission in error, please contact the sender immediately. Zelis, 2018.
Morning Bill, I take it this is dplyr? You might try: tmp1 <- HCPC %>% group_by(HCPCSCode) %>% summarise(Avg_AllowByLimit mean(Avg_AllowByLimit[which(Avg_AllowByLimit!=0 & AllowByLimitFlag =TRUE)])) The code above gives "NaN" for cases where AllowByLimitFlag == FALSE. Maybe this is the answer you desire, otherwise you can filter out "NaN" rows. Cheers, Bill. W. Michels, Ph.D. On Wed, May 22, 2019 at 5:41 AM Bill Poling <Bill.Poling at zelis.com> wrote:> > Good morning. > > #R version 3.6.0 Patched (2019-05-19 r76539) > #Platform: x86_64-w64-mingw32/x64 (64-bit) > #Running under: Windows >= 8 x64 (build 9200) > > I need a calculated field For the Rate of Avg_AllowByLimit where the Allowed_AmtFlag = TRUE BY Each Code > > I have almost got this. > > #So far I have this > 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 > > I cannot seem to get it in there correctly > > Thank you for any help > > WHP > > #Here is some data > HCPCSCode Avg_AllowByLimit AllowByLimitFlag > 1 J1745 4.50 FALSE > 2 J9299 18.70 FALSE > 3 J9306 14.33 FALSE > 4 J9355 7.13 FALSE > 5 J0897 8.61 FALSE > 6 J9034 3.32 FALSE > 7 J9034 3.32 FALSE > 8 J9045 15.60 FALSE > 9 J9035 2.77 TRUE > 10 J1190 3.62 FALSE > 11 J2250 879.10 FALSE > 12 J9033 2.92 FALSE > 13 J1745 4.50 TRUE > 14 J2785 12.11 FALSE > 15 J9045 15.60 FALSE > 16 J2350 7.81 FALSE > 17 J2469 10.65 TRUE > 18 J2796 6.27 FALSE > 19 J2796 6.27 FALSE > 20 J9355 7.13 FALSE > 21 J9045 15.60 FALSE > 22 J2505 2.73 FALSE > 23 J1786 2.81 FALSE > 24 J3262 3.26 FALSE > 25 J0696 168.87 FALSE > 26 J0178 1.52 TRUE > 27 J9271 5.55 FALSE > 28 J3380 80.99 FALSE > 29 J9355 7.13 TRUE > 30 J2469 10.65 FALSE > 31 J9045 15.60 FALSE > 32 J1459 3.64 FALSE > 33 J9305 8.74 FALSE > 34 J9034 3.32 FALSE > 35 J9034 3.32 FALSE > > Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}} > > ______________________________________________ > R-help at 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.
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 <ruipbarradas at 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 at 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.
Thank you William appreciate your response Sir. WHP From: William Michels <wjm1 at caa.columbia.edu> Sent: Wednesday, May 22, 2019 9:58 AM To: Bill Poling <Bill.Poling at zelis.com> Cc: r-help (r-help at r-project.org) <r-help at r-project.org> Subject: Re: [R] Help with R coding Morning Bill, I take it this is dplyr? You might try: tmp1 <- HCPC %>% group_by(HCPCSCode) %>% summarise(Avg_AllowByLimit mean(Avg_AllowByLimit[which(Avg_AllowByLimit!=0 & AllowByLimitFlag =TRUE)])) The code above gives "NaN" for cases where AllowByLimitFlag == FALSE. Maybe this is the answer you desire, otherwise you can filter out "NaN" rows. Cheers, Bill. W. Michels, Ph.D. On Wed, May 22, 2019 at 5:41 AM Bill Poling <mailto:Bill.Poling at zelis.com> wrote:> > Good morning. > > #R version 3.6.0 Patched (2019-05-19 r76539) > #Platform: x86_64-w64-mingw32/x64 (64-bit) > #Running under: Windows >= 8 x64 (build 9200) > > I need a calculated field For the Rate of Avg_AllowByLimit where the Allowed_AmtFlag = TRUE BY Each Code > > I have almost got this. > > #So far I have this > 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 > > I cannot seem to get it in there correctly > > Thank you for any help > > WHP > > #Here is some data > HCPCSCode Avg_AllowByLimit AllowByLimitFlag > 1 J1745 4.50 FALSE > 2 J9299 18.70 FALSE > 3 J9306 14.33 FALSE > 4 J9355 7.13 FALSE > 5 J0897 8.61 FALSE > 6 J9034 3.32 FALSE > 7 J9034 3.32 FALSE > 8 J9045 15.60 FALSE > 9 J9035 2.77 TRUE > 10 J1190 3.62 FALSE > 11 J2250 879.10 FALSE > 12 J9033 2.92 FALSE > 13 J1745 4.50 TRUE > 14 J2785 12.11 FALSE > 15 J9045 15.60 FALSE > 16 J2350 7.81 FALSE > 17 J2469 10.65 TRUE > 18 J2796 6.27 FALSE > 19 J2796 6.27 FALSE > 20 J9355 7.13 FALSE > 21 J9045 15.60 FALSE > 22 J2505 2.73 FALSE > 23 J1786 2.81 FALSE > 24 J3262 3.26 FALSE > 25 J0696 168.87 FALSE > 26 J0178 1.52 TRUE > 27 J9271 5.55 FALSE > 28 J3380 80.99 FALSE > 29 J9355 7.13 TRUE > 30 J2469 10.65 FALSE > 31 J9045 15.60 FALSE > 32 J1459 3.64 FALSE > 33 J9305 8.74 FALSE > 34 J9034 3.32 FALSE > 35 J9034 3.32 FALSE > > Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}} > > ______________________________________________ > mailto:R-help at 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.Confidentiality Notice This message is sent from Zelis. This transmission may contain information which is privileged and confidential and is intended for the personal and confidential use of the named recipient only. Such information may be protected by applicable State and Federal laws from this disclosure or unauthorized use. If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, review, discussion, copying, or taking any action in reliance on the contents of this transmission is strictly prohibited. If you have received this transmission in error, please contact the sender immediately. Zelis, 2018.