Ulrik Stervbo
2016-Jul-04 16:59 UTC
[R] dplyr : row total for all groups in dplyr summarise
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.[[alternative HTML version deleted]]
maicel at infomed.sld.cu
2016-Jul-04 17:46 UTC
[R] dplyr : row total for all groups in dplyr summarise
Thank you!!! It's what I needed. best regard Maicel Monzon, PHD Centro Nacional de Ensayos Clinicos Ulrik Stervbo <ulrik.stervbo at gmail.com> escribi?:> 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. >---------------------------------------------------------------- -- 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/
David Winsemius
2016-Jul-05 00:10 UTC
[R] dplyr : row total for all groups in dplyr summarise
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.[[alternative HTML version deleted]]
Ulrik Stervbo
2016-Jul-05 04:03 UTC
[R] dplyr : row total for all groups in dplyr summarise
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. > >[[alternative HTML version deleted]]