[英]How to use a variable in lm() function in R?
Let us say I have a dataframe (df) with two columns called "height" and "weight".假设我有一个 dataframe (df),有两列称为“高度”和“重量”。
Let's say I define:假设我定义:
x = "height"
How do I use x within my lm()
function?如何在我的lm()
function 中使用 x? Neither df[x]
nor just using x works. df[x]
和仅使用 x 都不起作用。
Two ways:两种方式:
Create a formula with paste
使用paste
创建公式
x = "height"
lm(paste0(x, '~', 'weight'), df)
Or use reformulate
或使用reformulate
lm(reformulate("weight", x), df)
Using reproducible example with mtcars
dataset:使用mtcars
数据集的可重现示例:
x = "Cyl"
lm(paste0(x, '~', 'mpg'), data = mtcars)
#Call:
#lm(formula = paste0(x, "~", "mpg"), data = mtcars)
#Coefficients:
#(Intercept) mpg
# 11.2607 -0.2525
and same with和一样
lm(reformulate("mpg", x), mtcars)
We can use glue
to create the formula我们可以使用glue
来创建公式
x <- "height"
lm(glue::glue('{x} ~ weight'), data = df)
Using a reproducible example with mtcars
使用mtcars
的可重现示例
x <- 'cyl'
lm(glue::glue('{x} ~ mpg'), data = mtcars)
#Call:
#lm(formula = glue::glue("{x} ~ mpg"), data = mtcars)
#Coefficients:
#(Intercept) mpg
# 11.2607 -0.2525
When you run x = "height"
your are assigning a string of characters to the variable x
.当您运行x = "height"
时,您将一串字符分配给变量x
。
Consider this data frame:考虑这个数据框:
df <- data.frame(
height = c(176, 188, 165),
weight = c(75, 80, 66)
)
If you want a regression using height
and weight
you can either do this:如果您想使用height
和weight
进行回归,您可以这样做:
lm(height ~ weight, data = df)
# Call:
# lm(formula = height ~ weight, data = df)
#
# Coefficients:
# (Intercept) weight
# 59.003 1.593
or this:或这个:
lm(df$height ~ df$weight)
# Call:
# lm(formula = df$height ~ df$weight)
#
# Coefficients:
# (Intercept) df$weight
# 59.003 1.593
If you really want to use x
instead of height
, you must have a variable called x
(in your df
or in your environment).如果您真的想使用x
而不是height
,则必须有一个名为x
的变量(在您的df
或您的环境中)。 You can do that by creating a new variable:您可以通过创建一个新变量来做到这一点:
x <- df$height
y <- df$weight
lm(x ~ y)
# Call:
# lm(formula = x ~ y)
#
# Coefficients:
# (Intercept) y
# 59.003 1.593
Or by changing the names of existing variables:或者通过更改现有变量的名称:
names(df) <- c("x", "y")
lm(x ~ y, data = df)
# Call:
# lm(formula = x ~ y, data = df)
#
# Coefficients:
# (Intercept) y
# 59.003 1.593
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.