使用 PLYR 来计算哪个条件
Using to PLYR to count with Which Condition
我正在尝试将 which 函数与 count 函数结合使用。我想计算遵循哪个条件的因素的数量。此代码不正确,但如有任何建议,我们将不胜感激。
library(plyr)
count(data, 'factor', which numeric > 10)
#Base version attempt
count(data$factor, which(data$numeric > 10))
Error in UseMethod("group_by_") :
no applicable method for 'group_by_' applied to an object of class "factor"
这不是您要找的东西,但这里有两条建议:
plyr
是 dplyr
的旧版本,所以我会使用较新的版本,尤其是因为它属于 tidyverse 组。 dplyr's
count可以处理因子
- 因子在 R 中不再常用。我建议只用
as.character
强制
使用 dplyr 你可以这样写:
data %>% filter(numeric > 10) %>% count(factor)
我正在尝试将 which 函数与 count 函数结合使用。我想计算遵循哪个条件的因素的数量。此代码不正确,但如有任何建议,我们将不胜感激。
library(plyr)
count(data, 'factor', which numeric > 10)
#Base version attempt
count(data$factor, which(data$numeric > 10))
Error in UseMethod("group_by_") :
no applicable method for 'group_by_' applied to an object of class "factor"
这不是您要找的东西,但这里有两条建议:
plyr
是dplyr
的旧版本,所以我会使用较新的版本,尤其是因为它属于 tidyverse 组。dplyr's
count可以处理因子- 因子在 R 中不再常用。我建议只用
as.character
强制
使用 dplyr 你可以这样写:
data %>% filter(numeric > 10) %>% count(factor)