简体   繁体   English

R-如何在函数中使用列名作为参数并插入模型公式

[英]R - how to use column names as arguments in a function and insert into a model formula

I want a function where the arguments can take a variable name (that is part of a dataset but is not stored as an object in the environment) and inserts that variable name into a model formula. 我想要一个函数,其中参数可以采用变量名(该变量名是数据集的一部分,但不存储为环境中的对象),并将该变量名插入模型公式中。

For example: 例如:

# Some data with a couple of variables
my_df <- data.frame(y = rbinom(10, 1,0.5), var1 = runif(10), var2 = runif(10))

# A function that fits a model using predictor specified in the arguments
my_fun <- function(var_name, df){
    glm(y ~ var_name, data = df, family = "binomial")
}

When I try to use the function I get the following error message 当我尝试使用该功能时,出现以下错误信息

my_fun(var1, my_df)
Error in eval(expr, envir, enclos) : object 'var1' not found 

# What I want the function to do
glm(y ~ var1, data = my_df, family = "binomial")

Is there a way to get this kind of function to work? 有没有办法让这种功能起作用?

You can parse unquoted var_name with substitute : 你可以不带引号的解析var_namesubstitute

my_fun <- function(var_name, df){
    glm.formula <- substitute(y ~ x, list(x = substitute(var_name)))
    glm(glm.formula, data = df, family = "binomial")
}

An example: 一个例子:

my_fun(var1, my_df)

# Call:  glm(formula = glm.formula, family = "binomial", data = df)
# 
# Coefficients:
# (Intercept)         var1  
#      -1.226        3.108  
# 
# Degrees of Freedom: 9 Total (i.e. Null);  8 Residual
# Null Deviance:        13.46 
# Residual Deviance: 11.35  AIC: 15.35

glm(y ~ var1, data = my_df, family = "binomial")

# Call:  glm(formula = y ~ var1, family = "binomial", data = my_df)
# 
# Coefficients:
# (Intercept)         var1  
#      -1.226        3.108  
# 
# Degrees of Freedom: 9 Total (i.e. Null);  8 Residual
# Null Deviance:        13.46 
# Residual Deviance: 11.35  AIC: 15.35

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在公式中将列名称传递为 function arguments - Pass column names as function arguments in formula 如何在R的函数中使用不同的列名 - How to use different column names in a function in R 将列名作为函数参数传递-R - Pass column names as function arguments - R 如何在 function 调用中使用 arguments 作为 R 中数据帧中的名称? - How to use arguments in a function call as names in dataframes in R? 是否可以定义一个“函数”并将其插入“公式”以在R中构造一个“ model.frame”? - Is it possible to define a `function` and insert it in `formula` to construct a `model.frame` in R? 如何在 r 的小标题中插入新的列名? - How to insert new column names to a tibble in r? R:调用 function,data.frame 列名称为 arguments - R: call function with data.frame column names as arguments 如何从R函数以数据框和列名作为参数创建自定义json输出 - how do you create a custom json output from an R function with dataframe and column names as arguments R,如何把一个dataframe的所有列名放到一个公式中? - R, how to put all the column names of a dataframe into a formula? 如何编写具有未指定数量的参数的函数,其中参数是列名 - How to write a function with an unspecified number of arguments where the arguments are column names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM