[R] Evaluate variable

Nic Rivers njrivers at sfu.ca
Fri Apr 9 21:06:09 CEST 2010


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



More information about the R-help mailing list