如何在手动创建的函数中使用变量名作为参数?

How do I use a variable name as an argument in a manually created function?

我有2个数据框:

df <- data.frame (model = c("A","A","A","B","B","B"),
                  category  = c("z3","f4","c5","d3","g6","B6"),
                  sale = c(1001,1050,-300,-150,-25,960))


df2 <- data.frame (model = c("A","B","A","B","A","B","A"),
                  category = c("z3","f4","c5","d3","g6","B6","z3"))

我在哪里做一些计算:

#summerise by category and model
df.agg <-df %>% 
  group_by(category ,model) %>% 
  summarise(sale = sum(sale))

#Drop duplicated rows
df.clean <- df2[!duplicated(df2$category), ]

#merge 2 dataframe
df.merge <- merge(x = df.agg, y = df.clean, by = "category", all.x = TRUE)

然而,当我尝试创建一个函数并将变量 'category' 指定为参数时,它不起作用:

converte <- function(x,y,z) {
#summerise by category and model
df.agg <-x %>% 
  group_by(z, model) %>% 
  summarise(sale = sum(sale))

#Drop duplicated rows
df.clean <- y[!duplicated(y$z), ]

#merge 2 dataframe
df.merge <- merge(x = df.agg, y = df.clean, by = z, all.x = TRUE)
}


data.wto.agg <- converte(df,df2, category)

一些变化,

converte <- function(x,y,z) {
  #summerise by category and model
  df.agg <-x %>% 
    group_by(across(c({{z}}, model))) %>% 
    summarise(sale = sum(sale))
  #Drop duplicated rows
  df.clean <- y[!duplicated(y[[z]]), ]
  #merge 2 dataframe
  df.merge <- merge(x = df.agg, y = df.clean, by = z, all.x = TRUE)
  return(df.merge)
}


converte(df, df2, 'category')

`summarise()` has grouped output by 'category'. You can override using the `.groups` argument.
  category model.x sale model.y
1       B6       B  960       B
2       c5       A -300       A
3       d3       B -150       B
4       f4       A 1050       B
5       g6       B  -25       A
6       z3       A 1001       A