r - Dplyr 汇总列

标签 r dplyr

我有一个数据集

company_category_list Cluster
Biotechnology         1
Software              2
Biotechnology|Search  1
Biotechnology         1
Biotechnology         1
Enterprise Software   3
Software              2

我想获取按 Cluster 列分组的第一列的计数,因此使用以下代码:

library(dplyr)
CountSummary <-SFBay_2012 %>% 
group_by(Cluster) %>% 
summarise(company_category_list_Count = count_(company_category_list))

但出现以下错误:

Error: no applicable method for 'group_by_' applied to an object of class "factor"

有人可以帮忙吗? 提前致谢!!

最佳答案

我想我们需要

SFBay_2012 %>%
        group_by(Cluster) %>% 
        count(company_category_list)   
#   Cluster company_category_list     n
#    <int>                 <chr> <int>
#1       1         Biotechnology     3
#2       1  Biotechnology|Search     1
#3       2              Software     2
#4       3   Enterprise Software     1

或者

SFBay_2012 %>% 
      count(Cluster, company_category_list)
#  Cluster company_category_list     n
#    <int>                 <chr> <int>
#1       1         Biotechnology     3
#2       1  Biotechnology|Search     1
#3       2              Software     2
#4       3   Enterprise Software     1

或者

SFBay_2012 %>%
        group_by(Cluster, company_category_list) %>% 
        tally()
#   Cluster company_category_list     n
#     <int>                 <chr> <int>
#1       1         Biotechnology     3
#2       1  Biotechnology|Search     1
#3       2              Software     2
#4       3   Enterprise Software     1

或者

SFBay_2012 %>%
     group_by(Cluster, company_category_list) %>%
     summarise(n = n())

关于r - Dplyr 汇总列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38719757/

相关文章:

r - 在 x 首先超过 y 的组中过滤

r - 根据来自不同 DataFrame 的分组值更新 DataFrame

r - 加入R时总结

r - 使用 enquo 时如何测试变量类型?

r - 为什么 summarize() 中的 cur_data() 会返回 df_slice() 错误?

r - 如何从 R 生成报告质量表?

大数据分组的 R 引导统计

r - svyglm 和加权 glm 之间的区别

html - 从 html 表中提取链接

r - 在 ifelse() 语句内部和外部运行一行时的不同输出