gpt4 book ai didi

R dplyr : calling `count()` with variable name (now that `count_()` is deprecated)

转载 作者:行者123 更新时间:2023-12-04 02:36:08 27 4
gpt4 key购买 nike

我正在尝试使用 dplyrcount()使用动态变量名而不是列名。以前,我会使用 count_() ,但现在已弃用。什么是最好的替代品?

最小可重现示例:

library(dplyr)
df <- data.frame(id = 1:10, city = sample(c("London","Paris","Amsterdam"), 10, replace=TRUE))
colname <- "city"

这是我尝试过的:
df %>% count( city )  # desired output (works but isn't dynamic)
df %>% count( !!colname ) # doesn't work, makes it literally "city"
df %>% count( vars(colname) ) # doesn't work
df %>% count( eval(colname) ) # doesn't work either
df %>% count( eval(parse(text=colname)) ) # works, but is not 'dplyr' ?
df %>% count( eval(sym(colname)) ) # works, but using `sym` from 'rlang'
df %>% count( !!as.name(colname) ) # works, but using `as.name` from 'base'
df %>% count_( colname ) # works, but is deprecated

不确定上述任何一种方法是首选方法,还是完全不同的方法?

提前致谢!

附注。我找到了 as.name()解决方案 here .

最佳答案

development version of dplyr , 其中 will soon be released在 CRAN 版本 1.0 上,across function 似乎是合适的选择:

df %>% count(across(colname))

dplyr 的当前 CRAN 版本中, group_by_at()函数可以接受一个字符串,所以你可以这样做:
df %>% group_by_at(colname) %>% tally

如果有 count_at便利功能,自然的类似事情是:
df %>% count_at(colname)

但是 dplyr没有 count_at函数,所以不起作用。
group_by_at当与 vars 一起使用时,还可以处理字符串和名称的混合。 ,所以你可以这样做:
colname = "cyl"
mtcars %>% group_by_at(vars(colname, vs)) %>% tally
group_by_at将继续在 dplyr 工作1.0,因此您可以创建自己的 count_at功能,如果你愿意。这将适用于字符串、名称,甚至两者的混合:
count_at = function(data, ...) {
data %>% group_by_at(vars(...)) %>% tally
}

colname="city"
df %>% count_at(colname)

  city          n
* <fct> <int>
1 Amsterdam 3
2 London 3
3 Paris 4

mtcars %>% count_at("cyl", vs)

    cyl    vs     n
<dbl> <dbl> <int>
1 4 0 1
2 4 1 10
3 6 0 3
4 6 1 4
5 8 0 14

关于R dplyr : calling `count()` with variable name (now that `count_()` is deprecated),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61735329/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com