0

I noticed that when using the group_by statement with summarize, I get a warning that the 'regrouping is being overridden by the .groups argument'. I found one article online that seems to indicate that a group_by statement is no longer necessary -- one just needs to include a group argument with summarize. I would like to figure out how to make this work as I'm prepping an online tutorial for students, and the less code the better. But for me, it's not working. Below is my Reprex. I'm just trying to get the mean age of each gender

library(tidyverse)

femaledata <- data.frame(age = rnorm(n=5, mean = 29, sd = 4), gender = "female")
maledata <- data.frame(age = rnorm(n=5, mean = 37, sd = 6), gender = "male")
alldata <- bind_rows(femaledata, maledata)

summarydata <- alldata %>%
  summarize(gender, meanage = mean(age))

The summarydata dataframe should just have two rows (one for female and one for male) with the mean age for each. Instead my dataframe looks like this:

gender   meanage
female    32.6
female    32.6
female    32.6
female    32.6
female    32.6
male      32.6
male      32.6
male      32.6
male      32.6
male      32.6

I know the group_by statement isn't complicated, but if I could get rid of a line of code, all the better.

Thanks, Wythe

wythe4
  • 83
  • 4
  • 3
    NO NO NO. That is incorrect!!!. The group by is still needed. You just need to specify what to do with the groups at the end. eg `alldata%>%group_by(gender)%>%summarise(mean(age),.groups = "drop")` for dropping the groups – Onyambu Aug 04 '20 at 16:39

1 Answers1

0

The new .group option is not to replace the group_by function. The option is to let you set the way grouping should be handled.

  • "drop_last": dropping the last level of grouping. This was the only supported option before version 1.0.0.
  • "drop": All levels of grouping are dropped.
  • "keep": Same grouping structure as .data.
  • "rowwise": Each row is it's own group.

The warning message when no .group option is set can be turned off by setting dplyr.summarise.inform to false.

PingPong
  • 355
  • 2
  • 11