【问题标题】:Passing column names to a function, written in R, imported into a separate R Markdown/R File将列名传递给用 R 编写的函数,导入单独的 R Markdown/R 文件
【发布时间】:2021-09-29 01:52:24
【问题描述】:

好的,问题来了:我在 R 中编写了一个函数,它接受一个数据帧,然后使用 dplyr 和 tidyr 操作并返回另一个数据帧。

问题:我正在传递用引号、双引号或反引号 (``) 括起来的列名,但在这种情况下,R 会抛出一条错误消息,指出“错误:找不到对象‘对象名’”反引号或“错误:必须按在 .data 中找到的变量分组。* 未找到列 'column_variable`。”

第一个块:我通过 source() 导入的函数

cat_perc_by_dept <- function(data, group, category) {
  # Returns the percentage of each category for a specific group.  
  # Inputs: 
  #   data: the dataset with multiple groups and multiple categories for each group
  #   group: the specific group, eg department, that we want to focus on within group_col
  #   category: the category we want to focus on within a particular group
  data %>% 
    select(DeptSpecialty, category) %>% 
    filter(DeptSpecialty == group) %>% 
    group_by(DeptSpecialty) %>% 
    mutate(Total = n(), Instance = 1) %>% 
    group_by(category) %>% 
    summarise(Perc = sum(Instance) / Total) %>% 
    distinct()
}

第二个:我在主文件中用引号调用它的函数

cat_perc_by_dept(data = visit_data, group = "Bariatrics", category = "ChargeDiagnosisCode")

还有反引号:

cat_perc_by_dept(data = visit_data, group = "Bariatrics", category = `ChargeDiagnosisCode`)

如何以不引发上述错误的方式将列名传递给函数?

【问题讨论】:

    标签: r function dplyr tidyr


    【解决方案1】:

    如果输入是字符串,我们可以在group_byselect中添加across

    cat_perc_by_dept <- function(data, group, category) {
      # Returns the percentage of each category for a specific group.  
      # Inputs: 
      #   data: the dataset with multiple groups and multiple categories for each group
      #   group: the specific group, eg department, that we want to focus on within group_col
      #   category: the category we want to focus on within a particular group
      data %>% 
        select(DeptSpecialty, across(all_of(category))) %>% 
        filter(DeptSpecialty == group) %>% 
        group_by(DeptSpecialty) %>% 
        mutate(Total = n(), Instance = 1) %>% 
        group_by(across(all_of(category))) %>% 
        summarise(Perc = sum(Instance) / Total) %>% 
        distinct()
    }
    

    或者另一种选择是使用ensym 转换为symbol 并评估(!!),这可以灵活,因为可以传递不带引号和带引号的参数

    cat_perc_by_dept <- function(data, group, category) {
      # Returns the percentage of each category for a specific group.  
      # Inputs: 
      #   data: the dataset with multiple groups and multiple categories for each group
      #   group: the specific group, eg department, that we want to focus on within group_col
      #   category: the category we want to focus on within a particular group
    
      
     category <- rlang::ensym(category)
      data %>% 
        select(DeptSpecialty, !!category) %>% 
        filter(DeptSpecialty == group) %>% 
        group_by(DeptSpecialty) %>% 
        mutate(Total = n(), Instance = 1) %>% 
        group_by(!!category) %>% 
        summarise(Perc = sum(Instance) / Total) %>% 
        distinct()
    }
    

    【讨论】:

    • 谢谢!到目前为止,您的第二个解决方案似乎保持稳定。
    • @StanleyYu across 是在 1.0.0 左右引入的。如果您有旧版本的 dplyr,它可能无法工作
    • 我怀疑你对“跨越”的怀疑是有根据的。我之前试过了,我的代码失败了。
    猜你喜欢
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2012-06-07
    相关资源
    最近更新 更多