Search code examples
rformula

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


Solution

  • 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