简体   繁体   中英

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 

This can be managed as a pkg option:

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

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM