Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R – use function argument as variable name and string value

I’m trying to use an excel file to manage information on covariates to include in many various models, which are estimated on different subgroups of my data.

I’m hoping to create a function to make it easy to loop over these models, where the function takes as input the name of the model. The model name is used to select covariates from "modelinfo" and to select the sample from "modeldata"

However, I’m not sure how to use an argument for these two distinct purposes. The below minimum working example uses the argument "modelnum" to refer to columns in modelinfo and rows in modeldata, but I can only get it to recognize the argument as a string to restrict the data by rows.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can an argument be used both as a string and as a column name?

library(tidyverse) 

modelinfo <- tribble(
            ~covars,~`model 1`,~`model 2`,
            "x1"    ,  1        , 0,
            "x2"    , 0         , 1
)

modeldata <- tribble(
             ~x1,   ~x2,  ~model,
             3,      3,    "model 1",
             4,      1,    "model 2",
             1,      3,    "model 1",
             2,      4,    "model 3"
)

testfun <- function(modelnum){
  covars <- modelinfo %>% 
    filter({{modelnum}}==1)
  
  data <- modeldata %>% 
    filter(model==modelnum)
  
  output=c(covars,data)
  return(output)
  
}

testfun(modelnum="model 1")

>Solution :

Here, it may be better to use .data with [[ to subset instead of {{}} as the input passed is character string

testfun <- function(modelnum){
  covars <- modelinfo %>% 
    filter(.data[[modelnum]]==1)
  
  data <- modeldata %>% 
    filter(model==modelnum)
  
  output=c(covars,data)
  return(output)
  
}

-testing

testfun(modelnum="model 1")
$covars
[1] "x1"

$`model 1`
[1] 1

$`model 2`
[1] 0

$x1
[1] 3 1

$x2
[1] 3 3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading