Guillaume
Guillaume

Reputation: 167

Combine names of variables (in a formula) retrieved by grep in R

I'd like to use the output of grep directly in a formula. In other words, I use grep to retrieve the variables I want to select and store them in a vector.
The cool thing would be to be able to use this vector in a formula. As to say

var.to.retrieve <- grep(pattern="V", x=data)
lm(var.dep~var.to.retrieve)

but this doesn't work... I've tried the solution paste(var.to.retrieve, collapse="+") but this doesn't work either.

EDIT The solution could be

formula <- as.formula(paste(var.dep, paste(var.to.retrieve, collapse="+"), sep="~"))

but I cannot imagine there is no more elegant way to do it

Upvotes: 2

Views: 134

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226732

reformulate(var.to.retrieve, response = var.dep) is basically this.

var.dep <- "y"
var.to.retrieve <- LETTERS[1:10]
r1 <- reformulate(var.to.retrieve, response = var.dep)
r2 <- as.formula(
       paste(var.dep, 
         paste(var.to.retrieve, collapse = "+"),
          sep = "~")
      )
identical(r1,r2) ## TRUE

Upvotes: 1

Shawn Brar
Shawn Brar

Reputation: 1420

var_to_retrieve <- colnames(data)[grep(pattern = "V", x = colnames(data))]
lm(formula(paste(var.dep, paste(var_to_retrieve, collapse = "+"), sep = "~")),
   data = data)

Upvotes: 0

Related Questions