cyrusjan
cyrusjan

Reputation: 647

How to use a list of variable in the operation in a loop (or lapply)?

I would like to run a series of analysis using different variables from a list. These variables are in the same dataset.

sample data frame is as below

df <- data.frame(x1=runif(10),
             x2=runif(10),
             x3=runif(10),
             x4=runif(10),
             y=runif(10))

# I would like to use variables from this list
xlist <- c("x1","x2","x3")

summary<-NA

my thought is to use for loop or lapply, but it seems that both methods have the same problem when calling the variable from the list. The problem is that I use "var" in the loop to represent x1,x2,or x3, but functions like "lm" require an argument of "data", and the function will recognize var as the variable to call, instead of x1,x2,or x3.

Any idea how can I avoid this?

# using for loop
for (var in xlist) {
  model <- lm(y~var,data=df)
  temp <- data.frame(coef=model$coefficients[2])
  summary<- rbind(summary,temp)
}

# using lapply
func <- function(var){
  model <- lm(y~var,data=df)
  temp <- data.frame(coef=model$coefficients[2])
}

result <- lapply(xlist, func)

Upvotes: 0

Views: 114

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18425

Perhaps you need the formula function...

for (var in xlist) {
  form <- formula(paste0("y~",var))
  model <- lm(form,data=df)
  temp <- data.frame(coef=model$coefficients[2])
  summary<- rbind(summary,temp)
}

summary
         coef
x1  0.3626764
x2 -0.3194918
x3  0.1216511

It should work in exactly the same way with lapply (or you might prefer sapply).

Upvotes: 2

Related Questions