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()
  }
Nneka
  • 1,764
  • 2
  • 15
  • 39
  • `param <- unique(param)` would be wrong as the `param` is created within the `data` so you may need `param <- unique(data$param)` – akrun Apr 16 '20 at 18:22

1 Answers1

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))
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662