Search code examples
rdplyrgroup-bysummarize

Why i can use this code for a group by in Rstudio


         sexo      fecha colnames
96991 Hombres 2020-03-02     sexo
96992 Hombres 2020-03-02    fecha
96993 Hombres 2020-03-02     sexo
96994 Hombres 2020-03-02    fecha
96995 Hombres 2020-03-02     sexo
96996 Hombres 2020-03-02    fecha

I have this table and i want to gruop by "fecha" and count the "sexo" type, for example : The "fecha" 2020-03-02 it has 65 "hombre", 32 "mujeres" and 3 "Nan". In Rstudio.

I ma trying this code, but doesnt work:

na_Sexo_fecha %>% 
  group_by("fecha") %>% 
  summarize(contador = count(sexo))

Solution

  • We can use the n function to do counting in a group.

    na_Sexo_fecha %>% 
      group_by(fecha, sexo) %>% 
      summarize(contador = n())