You probably want to use substitute() to construct your formula and be
careful of the distinction between character strings and names
> substitute(foo ~ bar, list(foo = as.name("y"), bar =
as.name("x")))
y ~ x
On Fri, Apr 9, 2010 at 2:06 PM, Nic Rivers <njrivers at sfu.ca>
wrote:> Dear R-users:
>
> I would like to create a system of regression equations of length n, where
> the variables are drawn from a data frame. ?The result I would like is
given
> by the variable named "system" in the code below. ?However, when
I use a
> loop to create the system of equations, I cannot seem to get the
expressions
> to evaluate immediately, and so the variable "system2" does not
replicate
> the result I would like. ?Is there a way to force the evaluation of the
loop
> variable "i" (I have tried eval() and force() without success)?
>
> Thanks for your help,
>
> Nic Rivers
> Simon Fraser University
>
>
> dat <-
>
data.frame(lhs.1=rnorm(100,1,1),lhs.2=rnorm(100,2,1),rhs.1=rnorm(100,3,1),rhs.2=rnorm(100,4,1))
>
> lhs <- c("lhs.1","lhs.2")
> rhs <- c("rhs.1","rhs.2")
>
> n <- 2 # number of equations
> r <- vector("list",length=n)
>
> # create system of equations manually
> r[["1"]] <- dat[lhs[1]] ~ dat[rhs[1]]
> r[["2"]] <- dat[lhs[2]] ~ dat[rhs[2]]
> system <- c(r[["1"]],r[["2"]])
>
> # create system of equations with a loop
> system2 <- list()
> for (i in 1:n) {
> ? ? ? ?r[[i]] <- dat[lhs[i]] ~ dat[rhs[i]]
> ? ? ? ?system2 <- append(system2,r[i])
> ? ? ? ?}
>
> system
> system2
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>