我试图简单地使用count使用数据框和 $ 来调用变量的 1 个变量。我在做
count(customer_churn$Churn)
Run Code Online (Sandbox Code Playgroud)
在安装之前,dplyr我得到了以下信息:
计数错误(customer_churn$Churn):找不到函数“计数”
安装dplyr并调用库后,我得到:
UseMethod("summarise_") 中的错误:没有适用于 'summarise_' 的方法应用于类 "c('integer', 'numeric')" 的对象
然后我尝试使用summarise代替并得到相同的错误。
count需要一个 data.frame/tibble。根据?dplyr::count.
x - 一个 tbl() 来统计/计数。
第二个问题是安装后不加载包的错误。它可以通过调用library(dplyr)或显式使用来加载dplyr::count
library(dplyr)
customer_churn %>%
count(Churn)
Run Code Online (Sandbox Code Playgroud)
在base R,table可以应用于vector
table(customer_churn$Churn)
Run Code Online (Sandbox Code Playgroud)
set.seed(240)
customer_churn <- data.frame(Churn = sample(1:5, 50, replace =TRUE))
Run Code Online (Sandbox Code Playgroud)