R 和 tidyverse 向导们大家好,
我尝试计算星球大战数据集的行数,以了解我们通过变量“高度”和“质量”得到了多少个观测结果 。 我设法用这段代码得到它:
library(tidyverse)
starwars %>%
select(height, mass) %>%
drop_na() %>%
summarise(across(.cols = c(height, mass),
list(obs = ~ n(),
mean = mean,
sd = sd))) %>%
View()
我想用 count
函数替换 obs = ~ n()
并尝试了这个版本:
library(tidyverse)
starwars %>%
select(height, mass) %>%
drop_na() %>%
summarise(across(.cols = c(height, mass),
list(obs = count,
mean = mean,
sd = sd))) %>%
View()
但是它太简单了,经典:p
我收到此错误消息 --> View 中的错误:计算时出现问题 ..1 = across(...)
当我摆脱 View() 函数时,我收到了另一条错误消息 --> summarise()
中的错误:
!计算 ..1 = across(...)
时出现问题。
由across()
中的错误引起:
!计算列 height_obs
时出现问题。
由 UseMethod()
中的错误引起:
!没有适用于“c('integer', 'numeric')”类对象的“count”方法
所以,我有两个问题:
有人可以解释一下为什么该代码适用于
~ n()
但不适用于count
吗?在这种情况下是否可以使用
count
函数代替~ n()
?
抱歉,如果这是一个愚蠢的问题,但我只是尝试通过使用它来理解 across
和 count
函数。
最佳答案
在函数描述中,它说“df %>% count(a, b)
大致相当于df %>% group_by(a, b) %>% summarise( n = n())
”,所以我假设在 across
中使用 count()
会产生类似双重汇总命令的结果,因此在支持n()
。
编辑:在这里您可以在 G. Grothendieck 的评论中找到解决方案 What is the difference between n() and count() in R? When should one favour the use of either or both?
n()
返回一个数字count()
返回一个数据帧
关于r - 是否可以使用cross()中的count函数进行计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73138020/