简体   繁体   中英

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.

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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM