1

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')
  • Does this answer your question? [Pass a data.frame column name to a function](https://stackoverflow.com/questions/2641653/pass-a-data-frame-column-name-to-a-function) – Limey May 24 '22 at 07:56
  • Avoid `within` outside interactive use. – Roland May 24 '22 at 08:02

1 Answers1

0

Your problem is that category and model in function are character. You can use function get to use characters, but I don't know how the [] will work with get. I think the easiest way is to create text of code in function and run it.

rplce <- function(df, var1, var2) {
    text <- paste0("within(df, ", var2, "[", var1, " == 'A'] <- 'X7')")
    return(eval(parse(text = text)))
}

EDITED

Test:

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

microbenchmark(
    within = within(df, category[model == 'A'] <- 'X7'),
    rplce = rplce(df, 'model', 'category')
)

Unit: milliseconds
   expr    min      lq     mean  median      uq      max neval cld
 within 5.9886 6.22465 8.567339 6.48545 7.18370 125.7000   100   a
  rplce 5.9122 6.30595 9.052931 6.54480 7.60605 122.5111   100   a

As you can see eval(paese(text = ...)) is not bad method.