Jeff Newmiller
2016-Jul-06 15:24 UTC
[R] WG: Fw: Re: dplyr : row total for all groups in dplyr summarise
Cut and paste is not to blame... it is the use of word processing software rather than text editors for manipulating code that is the problem. Georg: note that plyr does not mix very well with dplyr... try to pick one and stick with it. -- Sent from my phone. Please excuse my brevity. On July 6, 2016 7:41:49 AM PDT, David L Carlson <dcarlson at tamu.edu> wrote:>It's the cut and paste monster. Somewhere along the way, the final " >got converted to ? which R does not see. > >> "100%? > >+ > "100%" >[1] "100%" > >------------------------------------- >David L Carlson >Department of Anthropology >Texas A&M University >College Station, TX 77840-4352 > > >-----Original Message----- >From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of >G.Maubach at weinwolf.de >Sent: Wednesday, July 6, 2016 3:34 AM >To: r-help at r-project.org >Subject: [R] WG: Fw: Re: dplyr : row total for all groups in dplyr >summarise > >Hi All, > >if I run the suggested code > >mtcars %>% > group_by (am, gear) %>% > summarise (n = n()) %>% > mutate(rel.freq = paste0(round(100 * n / sum(n), 0), "%")) %>% > ungroup() %>% > plyr::rbind.fill(data.frame(n = nrow(mtcars), rel.freq > "100%?)) > >I get > >> mtcars %>% >+ group_by (am, gear) %>% >+ summarise (n = n()) %>% >+ mutate(rel.freq = paste0(round(100 * n / sum(n), 0), "%")) %>% >+ ungroup() %>% >+ plyr::rbind.fill(data.frame(n = nrow(mtcars), rel.freq >+ "100%?)) > > > > >+ > > >R stops execution cause something within the prgram syntax is missing. > >What has to be changed to be able to run the code? > >Kind regards > >Georg Maubach > > >> Gesendet: Dienstag, 05. Juli 2016 um 18:30 Uhr >> Von: "David Winsemius" <dwinsemius at comcast.net> >> An: maicel at infomed.sld.cu >> Cc: r-help at r-project.org >> Betreff: Re: [R] dplyr : row total for all groups in dplyr summarise >> >> >> >> mtcars %>% >> group_by (am, gear) %>% >> summarise (n=n()) %>% >> mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%")) %>% >> ungroup() %>% plyr::rbind.fill(data.frame( >n=nrow(mtcars),rel.freq="100%?)) >> >> >> > On Jul 5, 2016, at 4:47 AM, maicel at infomed.sld.cu wrote: >> > >> > Sorry, what I wanted to do was to add a total row at the end of the > >summary. The marginal totals by columns correspond to 100% and the sum >of >levels. >> > best reagard >> > Maicel Monzon >> > >> > >> > Ulrik Stervbo <ulrik.stervbo at gmail.com> escribi?: >> > >> >> Yes. But in the sample code the data is summarised. In which case >you >get 4 >> >> rows and not the correct 32. >> >> >> >> On Tue, 5 Jul 2016, 07:48 David Winsemius, ><dwinsemius at comcast.net> >wrote: >> >> >> >>> nrow(mtcars) >> >>> >> >>> >> >>> Sent from my iPhone >> >>> >> >>> On Jul 4, 2016, at 9:03 PM, Ulrik Stervbo ><ulrik.stervbo at gmail.com> >wrote: >> >>> >> >>> That will give you the wrong result when used on summarised data >> >>> >> >>> David Winsemius <dwinsemius at comcast.net> schrieb am Di., 5. Juli >2016 >> >>> 02:10: >> >>> >> >>>> I thought there was an nrow() function? >> >>>> >> >>>> Sent from my iPhone >> >>>> >> >>>> On Jul 4, 2016, at 9:59 AM, Ulrik Stervbo ><ulrik.stervbo at gmail.com> >> >>>> wrote: >> >>>> >> >>>> If you want the total number of rows in the original data.frame >after >> >>>> counting the rows in each group, you can ungroup and sum the row > >counts, >> >>>> like: >> >>>> >> >>>> library("dplyr") >> >>>> >> >>>> >> >>>> mtcars %>% >> >>>> group_by (am, gear) %>% >> >>>> summarise (n=n()) %>% >> >>>> mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%")) %>% >> >>>> ungroup() %>% >> >>>> mutate(row.tot = sum(n)) >> >>>> >> >>>> HTH >> >>>> Ulrik >> >>>> >> >>>> On Mon, 4 Jul 2016 at 18:23 David Winsemius ><dwinsemius at comcast.net> >> >>>> wrote: >> >>>> >> >>>>> >> >>>>> > On Jul 4, 2016, at 6:56 AM, maicel at infomed.sld.cu wrote: >> >>>>> > >> >>>>> > Hello, >> >>>>> > How can I aggregate row total for all groups in dplyr >summarise >? >> >>>>> >> >>>>> Row total ? of what? Aggregate ? how? What is the desired >answer? >> >>>>> >> >>>>> >> >>>>> >> >>>>> > library(dplyr) >> >>>>> > mtcars %>% >> >>>>> > group_by (am, gear) %>% >> >>>>> > summarise (n=n()) %>% >> >>>>> > mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%")) >> >>>>> > >> >>>>> > best regard >> >>>>> > Maicel Monzon >> >>>>> > >> >>>>> > >> >>>>> > >> >>>>> > >---------------------------------------------------------------- >> >>>>> > >> >>>>> > >> >>>>> > >> >>>>> > >> >>>>> > -- >> >>>>> > Este mensaje le ha llegado mediante el servicio de correo >electronico >> >>>>> que ofrece Infomed para respaldar el cumplimiento de las >misiones >del >> >>>>> Sistema Nacional de Salud. La persona que envia este correo >asume >el >> >>>>> compromiso de usar el servicio a tales fines y cumplir con las >regulaciones >> >>>>> establecidas >> >>>>> > >> >>>>> > Infomed: http://www.sld.cu/ >> >>>>> > >> >>>>> > ______________________________________________ >> >>>>> > 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. >> >>>>> >> >>>>> ______________________________________________ >> >>>>> 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. >> >>>> >> >>>> >> >> >> > >> > >> > >> > ---------------------------------------------------------------- >> > This message was sent using IMP, the Internet Messaging Program. >> > >> > >> > >> > -- >> > Este mensaje le ha llegado mediante el servicio de correo >electronico >que ofrece Infomed para respaldar el cumplimiento de las misiones del >Sistema Nacional de Salud. La persona que envia este correo asume el >compromiso de usar el servicio a tales fines y cumplir con las >regulaciones establecidas >> > >> > Infomed: http://www.sld.cu/ >> > >> >> ______________________________________________ >> 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. >> > >______________________________________________ >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. >______________________________________________ >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.
David Winsemius
2016-Jul-06 16:36 UTC
[R] dplyr : row total for all groups in dplyr summarise
> On Jul 6, 2016, at 8:24 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: > > Cut and paste is not to blame... it is the use of word processing software rather than text editors for manipulating code that is the problem.In this case the text was cut from the R session console text and pasted without modification into Mail.app version 8.2. In replicating this action, I see now that hitting "return" then unfortunately converts the final double-quote to a "smart-quote". So the fault is mine because I'm responsible for my software choices.> > Georg: note that plyr does not mix very well with dplyr... try to pick one and stick with it.Agreed. Note that I did not load plyr, but rather called a single function from it. Looking at my sessionInfo() from that session, I see that plyr was loaded via NAMESPACE and I'm guessing that was due to having loaded ggplot2 (which many people would also have). However, I'm guessing that `rbind.fill` would fail if the plyr NAMESPACE links were not available since it is dependent on several other plyr functions. -- David.> -- > Sent from my phone. Please excuse my brevity. > > On July 6, 2016 7:41:49 AM PDT, David L Carlson <dcarlson at tamu.edu> wrote: >> It's the cut and paste monster. Somewhere along the way, the final " >> got converted to ? which R does not see. >> >>> "100%? >> >> + > "100%" >> [1] "100%" >> >> ------------------------------------- >> David L Carlson >> Department of Anthropology >> Texas A&M University >> College Station, TX 77840-4352 >> >> >> -----Original Message----- >> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of >> G.Maubach at weinwolf.de >> Sent: Wednesday, July 6, 2016 3:34 AM >> To: r-help at r-project.org >> Subject: [R] WG: Fw: Re: dplyr : row total for all groups in dplyr >> summarise >> >> Hi All, >> >> if I run the suggested code >> >> mtcars %>% >> group_by (am, gear) %>% >> summarise (n = n()) %>% >> mutate(rel.freq = paste0(round(100 * n / sum(n), 0), "%")) %>% >> ungroup() %>% >> plyr::rbind.fill(data.frame(n = nrow(mtcars), rel.freq >> "100%?)) >> >> I get >> >>> mtcars %>% >> + group_by (am, gear) %>% >> + summarise (n = n()) %>% >> + mutate(rel.freq = paste0(round(100 * n / sum(n), 0), "%")) %>% >> + ungroup() %>% >> + plyr::rbind.fill(data.frame(n = nrow(mtcars), rel.freq >> + "100%?)) >> >> >> >> >> + >> >> >> R stops execution cause something within the prgram syntax is missing. >> >> What has to be changed to be able to run the code? >> >> Kind regards >> >> Georg Maubach >> >> >>> Gesendet: Dienstag, 05. Juli 2016 um 18:30 Uhr >>> Von: "David Winsemius" <dwinsemius at comcast.net> >>> An: maicel at infomed.sld.cu >>> Cc: r-help at r-project.org >>> Betreff: Re: [R] dplyr : row total for all groups in dplyr summarise >>> >>> >>> >>> mtcars %>% >>> group_by (am, gear) %>% >>> summarise (n=n()) %>% >>> mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%")) %>% >>> ungroup() %>% plyr::rbind.fill(data.frame( >> n=nrow(mtcars),rel.freq="100%?)) >>> >>> >>>> On Jul 5, 2016, at 4:47 AM, maicel at infomed.sld.cu wrote: >>>> >>>> Sorry, what I wanted to do was to add a total row at the end of the >> >> summary. The marginal totals by columns correspond to 100% and the sum >> of >> levels. >>>> best reagard >>>> Maicel Monzon >>>> >>>> >>>> Ulrik Stervbo <ulrik.stervbo at gmail.com> escribi?: >>>> >>>>> Yes. But in the sample code the data is summarised. In which case >> you >> get 4 >>>>> rows and not the correct 32. >>>>> >>>>> On Tue, 5 Jul 2016, 07:48 David Winsemius, >> <dwinsemius at comcast.net> >> wrote: >>>>> >>>>>> nrow(mtcars) >>>>>> >>>>>> >>>>>> Sent from my iPhone >>>>>> >>>>>> On Jul 4, 2016, at 9:03 PM, Ulrik Stervbo >> <ulrik.stervbo at gmail.com> >> wrote: >>>>>> >>>>>> That will give you the wrong result when used on summarised data >>>>>> >>>>>> David Winsemius <dwinsemius at comcast.net> schrieb am Di., 5. Juli >> 2016 >>>>>> 02:10: >>>>>> >>>>>>> I thought there was an nrow() function? >>>>>>> >>>>>>> Sent from my iPhone >>>>>>> >>>>>>> On Jul 4, 2016, at 9:59 AM, Ulrik Stervbo >> <ulrik.stervbo at gmail.com> >>>>>>> wrote: >>>>>>> >>>>>>> If you want the total number of rows in the original data.frame >> after >>>>>>> counting the rows in each group, you can ungroup and sum the row >> >> counts, >>>>>>> like: >>>>>>> >>>>>>> library("dplyr") >>>>>>> >>>>>>> >>>>>>> mtcars %>% >>>>>>> group_by (am, gear) %>% >>>>>>> summarise (n=n()) %>% >>>>>>> mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%")) %>% >>>>>>> ungroup() %>% >>>>>>> mutate(row.tot = sum(n)) >>>>>>> >>>>>>> HTH >>>>>>> Ulrik >>>>>>> >>>>>>> On Mon, 4 Jul 2016 at 18:23 David Winsemius >> <dwinsemius at comcast.net> >>>>>>> wrote: >>>>>>> >>>>>>>> >>>>>>>>> On Jul 4, 2016, at 6:56 AM, maicel at infomed.sld.cu wrote: >>>>>>>>> >>>>>>>>> Hello, >>>>>>>>> How can I aggregate row total for all groups in dplyr >> summarise >> ? >>>>>>>> >>>>>>>> Row total ? of what? Aggregate ? how? What is the desired >> answer? >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>>> library(dplyr) >>>>>>>>> mtcars %>% >>>>>>>>> group_by (am, gear) %>% >>>>>>>>> summarise (n=n()) %>% >>>>>>>>> mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%")) >>>>>>>>> >>>>>>>>> best regard >>>>>>>>> Maicel Monzon >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >> ---------------------------------------------------------------- >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> -- >>>>>>>>> Este mensaje le ha llegado mediante el servicio de correo >> electronico >>>>>>>> que ofrece Infomed para respaldar el cumplimiento de las >> misiones >> del >>>>>>>> Sistema Nacional de Salud. La persona que envia este correo >> asume >> el >>>>>>>> compromiso de usar el servicio a tales fines y cumplir con las >> regulaciones >>>>>>>> establecidas >>>>>>>>> >>>>>>>>> Infomed: http://www.sld.cu/ >>>>>>>>> >>>>>>>>> ______________________________________________ >>>>>>>>> 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. >>>>>>>> >>>>>>>> ______________________________________________ >>>>>>>> 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. >>>>>>> >>>>>>> >>>>> >>>> >>>> >>>> >>>> ---------------------------------------------------------------- >>>> This message was sent using IMP, the Internet Messaging Program. >>>> >>>> >>>> >>>> -- >>>> Este mensaje le ha llegado mediante el servicio de correo >> electronico >> que ofrece Infomed para respaldar el cumplimiento de las misiones del >> Sistema Nacional de Salud. La persona que envia este correo asume el >> compromiso de usar el servicio a tales fines y cumplir con las >> regulaciones establecidas >>>> >>>> Infomed: http://www.sld.cu/ >>>> >>> >>> ______________________________________________ >>> 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. >>> >> >> ______________________________________________ >> 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. >> ______________________________________________ >> 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. > > ______________________________________________ > 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.David Winsemius Alameda, CA, USA
rmendelss gmail
2016-Jul-06 16:45 UTC
[R] dplyr : row total for all groups in dplyr summarise
> On Jul 6, 2016, at 9:36 AM, David Winsemius <dwinsemius at comcast.net> wrote: > > n this case the text was cut from the R session console text and pasted without modification into Mail.app version 8.2. In replicating this action, I see now that hitting "return" then unfortunately converts the final double-quote to a "smart-quote?.Speaking of this, this happens to me a lot when I post, so any example code can not be copied and paste to be reproducible. Does anyone know of a workaround for this? Thanks, -Roy