【问题标题】:How can I use column number for dplyr::count()?如何为 dplyr::count() 使用列号?
【发布时间】:2017-01-16 01:11:58
【问题描述】:
library(dplyr)

没关系。

count(iris, Species) 
# A tibble: 3 × 2
     Species     n
      <fctr> <int>
1     setosa    50
2 versicolor    50
3  virginica    50
> names(iris)[5]
[1] "Species"

这不起作用。为什么? 如何使用 dplyr::count() 的列号?

> count(iris, one_of(names(iris)[5])) 
    Error in mutate_impl(.data, dots) : 
  wrong result size (0), expected 150 or 1
In addition: Warning message:
In one_of(names(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4,  :
  Unknown variables: `Species`

【问题讨论】:

  • 尝试使用count_ 函数。如:count_(iris, names(iris)[2])

标签: r dplyr


【解决方案1】:

count(iris, iris[,5]) 应该会产生与您相同的结果。 不确定one_of,但如果你删除它并在[5]中添加,,它应该会产生没有错误的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2020-03-06
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    相关资源
    最近更新 更多