[R] building a formula string bit by bit ..

Esmail Bonakdarian esmail.js at gmail.com
Tue May 6 21:43:19 CEST 2008


Jorge Ivan Velez wrote:
> Hi Esmail,
> 
> Try this:
> 
> vars=c('X.1', 'X.2', 'X.3', 'X.4', 'X.5')
> bits=c(1, 0, 1, 1, 0)
> paste(vars[which(bits==1)],collapse="+")
> 
> HTH,
> 
> Jorge

Wow .. that is beautiful :-) .. and exactly what I was looking
for (and suspected existed).

I ended up doing this:

    eqn=(paste(vars[which(bits==1)],collapse=" + "))
    eqn=paste(c("Y.data ~"), eqn)
    GLM.9 <- glm(as.formula(eqn) , family=gaussian(identity), data=simpleData)

For some reason I couldn't collapse the two eqn/paste statements into one
statement but this seems to work.

Thank again Jorge and everyone else .. this group is a big help.

Esmail



More information about the R-help mailing list