[R] Splitting the left and right hand terms of a formula

Daniel Gerlanc dgerlanc at gmail.com
Fri Jul 14 20:23:30 CEST 2006


Let's say I have the following formula:

a.formula <- x ~ y + z

I want to extract the left and right-hand sides of the function so
that I have two character vectors like the ones you would create using
the following assignments:

left.hand.side <- "x"
right.hand.side <- c("y", "z")

One way to do this follows:

left.hand.side <- unlist(dimnames(attr(terms(a.formula), "factors"))[1])
right.hand.side <- unlist(dimnames(attr(terms(a.formula), "factors"))[-1])

Is there a better or cleaner way to do this?

Thanks!

Daniel Gerlanc
Williams College '07



More information about the R-help mailing list