在函数中按列名称选择

问题描述 投票:0回答:1

我感觉我快失去理智了。这应该有效吧?

选择在数据集中通过管道传输的变量。我收到的错误是

Error: object 'cyl' not found

table_fun = function(data, grouping_var){
  data %>% 
    select(grouping_var)%>%
    group_by(grouping_var)%>%
    summarise(count = n())
}

table_fun(mtcars, cyl)
r function tidy
1个回答
0
投票
table_fun = function(data, grouping_var){
  grouping_var <-  deparse(substitute(grouping_var))
  data %>% 
    select(data[[grouping_var]])%>%
    group_by(data[[grouping_var]])%>%
    summarise(count = n())
}


© www.soinside.com 2019 - 2024. All rights reserved.