0

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)
user438383
  • 5,716
  • 8
  • 28
  • 43

1 Answers1

1

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
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • Do you know how to specify the code within function: when var1 = value, replace value in var2 by NEWvar2value? Because this code does not work: df <- within(df, var2[var1 == 'value] <- 'NEWvar2value') –  May 24 '22 at 07:31
  • Try `df$var2 <- replace(df$var2, df$var1 == value, Newvar2Value[df$var1 == value])` – Sotos May 24 '22 at 07:59