1

as a part of a bigger function, i need to create two new columns in a data.table (which is later on used to create a plot).

these are the names of my columns:

names(freqSevDataAge)
 [1] "ag5"                           "claims"              "exposure"               
 [7] "severity"             "frequency" 

I am trying to make this part of the function work:

  testDT <- function(data, xvar, yvar, yvarsec, groupvar, ...){

  freqSevDataAge2 <- freqSevDataAge[!claims == 0][, ':=' (scaled = "yvarsec" * max("yvar")/max("yvarsec"),
                                                          param  = max("yvar")/max("yvarsec"))]
  }

  testDT(freqSevDataAge, xvar = "ag5", yvar = "severity", yvarsec = "frequency", groupvar = "gender")

the error i get is:

Error in "yvarsec" * max("yvar") : non-numeric argument to binary operator

EDIT:

The prposed solution using get() worked, however now i am having troubles using the newly creates column in the ggplot . I get an error:

Error in f(...) : object 'param' not found

i checked the function step by stem and know that the column param is created, the problem is calling it in the ggplot. How can i

getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){

  if ("agegroup" %in% xvar) xvar <- get("agegroup")

  data <- data[!claims == 0][, ':=' (scaled = get(yvarsec) * max(get(yvar))/max(get(yvarsec)),
                                     param  = max(get(yvar))/max(get(yvarsec)))]

param <- unique(param)

  sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
      geom_col(aes_string(y = yvar, fill = groupvar, alpha = 0.5), position = "dodge") +
      geom_line(aes(y = scaled,  color = gender)) +
      scale_y_continuous(sec.axis = sec_axis(~./(param),
                                             name = paste0("average ", yvarsec), labels = function(x) format(x, big.mark = " ", scientific = FALSE))) +
      labs(y = paste0("total ", yvar)) +
      theme_pubclean()
  }
1
  • param <- unique(param) would be wrong as the param is created within the data so you may need param <- unique(data$param) Commented Apr 16, 2020 at 18:22

1 Answer 1

1

We could change the function by removing the quotes from the variables within the function, use get to get the value of the object

library(data.table)
testDT <- function(data, xvar, yvar, yvarsec, groupvar, ...){

   freqSevDataAge[!claims == 0][, ':=' 
    (scaled = get(yvarsec) * max(get(yvar))/max(get(yvarsec)),
                       param  = max(get(yvar))/max(get(yvarsec)))][]
    }


testDT(freqSevDataAge, xvar = "ag5", yvar = "severity", 
             yvarsec = "frequency", groupvar = "gender")
#    ag5 severity frequency gender claims     scaled     param
#1:   3        8         9      M      1  3.7500000 0.4166667
#2:   6        3        24      F      1 10.0000000 0.4166667
#3:   7       10        17      F      1  7.0833333 0.4166667
#4:   8        8         8      M      1  3.3333333 0.4166667
#5:  10       10         1      M      1  0.4166667 0.4166667

Or another option is to convert to symbol with as.symbol and evaluate with eval

data

set.seed(24)
freqSevDataAge <- data.table(ag5 = 1:10, severity = sample(1:10, 10,
   replace = TRUE), frequency = sample(1:24, 10, replace = TRUE),
   gender = sample(c("M", "F"), 10, replace = TRUE), 
   claims = sample(0:1, 10, replace = TRUE))
Sign up to request clarification or add additional context in comments.

4 Comments

that is what i had initially, but was getting an error : ` Error in yvarsec * max(yvar) : non-numeric argument to binary operator `
@Danka have you used get
@Danka you can check my reproducible example with output. It could be also that some of your columns are not numeric class
I added a related question to my post, maybe you have ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.