dplyr message: Adding missing grouping variable

dplyr: Adding missing grouping variable

The dplyr message about adding missing grouping variable might appear during a data wrangling process. In my case, when I was using function select from dplyr, the “Adding missing grouping variable” message was displayed, and an unnecessary column appeared in the result. You cannot remove this column as easily as usual.

Here is an example where you can see the message about adding a missing grouping variable. It is normal behavior with the column that was used in the group_by function.

require(dplyr)

airquality %>%
  group_by(Month) %>%
  mutate("Mean_Temp" = mean(Temp)) %>%
  filter(Month == 5) %>%
  select(Day, Temp, Mean_Temp) %>% 
  as.data.frame() %>% 
  head()

#Adding missing grouping variables: `Month`
#  Month Day Temp Mean_Temp
#1     5   1   67  65.54839
#2     5   2   72  65.54839
#3     5   3   74  65.54839
#4     5   4   62  65.54839
#5     5   5   56  65.54839
#6     5   6   66  65.54839

In this case, a grouping variable is necessary for the subsequent process. Fortunately, the solution is simple. To remove the grouping variable in dplyr, try to use ungroup function. Here it is in action.

airquality %>%
  group_by(Month) %>%
  mutate("Mean_Temp" = mean(Temp)) %>%
  filter(Month == 5) %>%
  ungroup() %>% 
  select(Day, Temp, Mean_Temp) %>% 
  as.data.frame() %>% 
  head()

#  Day Temp Mean_Temp
#1   1   67  65.54839
#2   2   72  65.54839
#3   3   74  65.54839
#4   4   62  65.54839
#5   5   56  65.54839
#6   6   66  65.54839

As you can see in the results, the function ungroup removes the grouping, and the result does not contain an unnecessary variable.

In other situations, you can drop the column in R data frame as usual.

By the way, there are some dplyr tips and tricks that might be interesting for you. In the select function, you can also rename or remove columns. By knowing this and other tips, you can improve your experience in working with data.

If you are an RStudio user, take a look at my favorite RStudio tips and tricks.




0 comments on “dplyr: Adding missing grouping variable

Leave a Reply

Your email address will not be published.