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" <[email protected]>
> An: [email protected]
> Cc: [email protected]
> 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, [email protected] 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 <[email protected]> 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, <[email protected]> 
wrote:
> >> 
> >>> nrow(mtcars)
> >>> 
> >>> 
> >>> Sent from my iPhone
> >>> 
> >>> On Jul 4, 2016, at 9:03 PM, Ulrik Stervbo <[email protected]> 
wrote:
> >>> 
> >>> That will give you the wrong result when used on summarised data
> >>> 
> >>> David Winsemius <[email protected]> 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 <[email protected]>
> >>>> 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 
<[email protected]>
> >>>> wrote:
> >>>> 
> >>>>> 
> >>>>> > On Jul 4, 2016, at 6:56 AM, [email protected] 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/
> >>>>> >
> >>>>> > ______________________________________________
> >>>>> > [email protected] 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.
> >>>>> 
> >>>>> ______________________________________________
> >>>>> [email protected] 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/
> > 
> 
> ______________________________________________
> [email protected] 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.
>

______________________________________________
[email protected] 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.

Reply via email to