Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate the ungroup... message from tidyverse package

When I run the following command I get an annoying message that says: summarise() ungrouping output (override with .groups argument).

I was wondering how I can eliminate this message in my data below?

library(tidyverse)

hsb <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/hsb.csv')  
ave_cluster_n <- as_vector(hsb %>% dplyr::select(sch.id) %>% group_by(sch.id) %>% summarise(n=n()) %>%  ungroup() %>% dplyr::select(n))

# `summarise()` ungrouping output (override with `.groups` argument) # How to eliminate this message 
like image 596
rnorouzian Avatar asked Sep 26 '20 21:09

rnorouzian


2 Answers

This can be managed as a pkg option:

library(tidyverse)
options(dplyr.summarise.inform = FALSE)

... and you don't see those messages anymore

like image 79
PinkCollins Avatar answered Nov 02 '22 23:11

PinkCollins


We can specify the .groups argument in summarise with different options if we want to avoid getting the message. Also, to extract as a vector, in the tidyverse, there is pull to pull the column

library(dplyr)
hsb %>% 
    dplyr::select(sch.id) %>%
    group_by(sch.id) %>%
    summarise(n=n(), .groups = 'drop') %>%
    pull(n)

Or another option is to bypass the group_by/summarise altogether and use count

hsb %>%
   count(sch.id) %>%
   pull(n)

Or with tally

hsb %>%
   group_by(sch.id) %>% 
   tally()
like image 35
akrun Avatar answered Nov 02 '22 22:11

akrun