好吧,问题来了:我在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`)
如何以不引发上述错误的方式将列名传递给函数?
如果输入是字符串,我们可以在group_by
和select
中添加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
转换为sym
bol并求值(!!
(,这可以是灵活的,因为未引用和引用的参数都可以传递
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()
}