Home > Software design >  How do I use a variable name as an argument in a manually created function?
How do I use a variable name as an argument in a manually created function?

Time:05-24

I have 2 data frame:

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"))

where I do some calculation:

#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)

However when I am trying to create a function and specify the variable 'category' as an argument, it does not work:

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)

CodePudding user response:

A couple of changes,

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
  • Related