【问题标题】:Eliminate the ungroup... message from tidyverse package消除 tidyverse 包中的 ungroup... 消息
【发布时间】:2023-04-03 06:34:01
【问题描述】:

当我运行以下命令时,我收到一条恼人的消息:summarise() ungrouping output (override with .groups argument)

我想知道如何在下面的数据中消除此消息?

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 

【问题讨论】:

    标签: r dataframe dplyr tidyverse


    【解决方案1】:

    如果我们想避免收到消息,我们可以使用不同的选项在summarise 中指定.groups 参数。另外,要提取为vector,在tidyverse中,有pull拉列

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

    或者另一种选择是完全绕过group_by/summarise并使用count

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

    或者tally

    hsb %>%
       group_by(sch.id) %>% 
       tally()
    

    【讨论】:

      【解决方案2】:

      这可以作为 pkg 选项进行管理:

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

      ...你再也看不到这些消息了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-25
        • 2022-06-18
        • 2018-06-29
        • 2021-02-10
        • 2012-04-25
        • 2020-12-03
        相关资源
        最近更新 更多