I am trying to replace existing value by X7 in 'category' column, when value in 'model' variable equal to A. However, I want to do that in manually created function. Outside function it works, but inside doesn't
Data:
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))
code outside the function:
df<- within(df, category[model == 'A'] <- 'X7')
Function:
rplce <- function(z,var1,var2) {
df <- within(df, var2[var1 == 'A'] <- 'X7')
return(df)
}
df2 <- rplce(data.wto.agg,'category','model')