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

Let's say I define:

x = "height"

How do I use x within my lm() function? Neither df[x] nor just using x works.

Upvotes: 5

Views: 1543

Answers (3)

Pedro Fonseca
Pedro Fonseca

Reputation: 331

When you run x = "height" your are assigning a string of characters to the variable 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:

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

Upvotes: 1

akrun
akrun

Reputation: 887118

We can use glue to create the formula

x <- "height"
lm(glue::glue('{x} ~ weight'), data = df)

Using a reproducible example with 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  

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 388982

Two ways :

Create a formula with paste

x = "height"
lm(paste0(x, '~', 'weight'), df)

Or use reformulate

lm(reformulate("weight", x), df)

Using reproducible example with mtcars dataset :

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)

Upvotes: 6

Related Questions