This has got to be incredibly simple but I nevertheless can't figure it out
as I am apparently brain dead.
I just want to convert the elements of a character vector to variable names,
so as to then assign formulas to them, e.g:
z = c(model1,model2); I want to assign formulas, such as lm(y~x[,1])
The usual response to this sort of question is usually something like the
following:
assign() will do what you want; get() runs the other direction. But the more
R way to do it is to put all the models in a list.
Michael
On Fri, Sep 23, 2011 at 1:03 PM, Jim Bouldin bouldi...@gmail.com wrote:
Jim Bouldin wrote on 09/23/2011 12:03:47 PM:
This has got to be incredibly simple but I nevertheless can't figure it
out
as I am apparently brain dead.
I just want to convert the elements of a character vector to variable
names,
so as to then assign formulas to them, e.g:
z =
Yes, I tried to do it using assign. I couldn't get that to work. E.g:
z=1:2; zz=rep(model,2);zzz = paste(zz,z,sep='');zzz
[1] model1 model2
y = 1:10; v = rnorm(10,0,2); x2 = y + v; x3 = y + v^0.5
x = data.frame(x2,x3)
for (i in 1:2){assign(zzz[i],lm(y~x[,i]))};zzz
[1] model1 model2
stumped
What exactly is the problem? Like I said, I'd personally put this in a list,
but this seems like exactly what you wanted...
model1
Call:
lm(formula = y ~ x[, i])
Coefficients:
(Intercept) x[, i]
1.0489 0.7175
model2
Call:
lm(formula = y ~ x[, i])
Coefficients:
(Intercept)
OK, I see. I thought R was just returning the character strings of the
model names without doing any assigning, since that's what it displayed. I
had it right all along. Thanks for your help.
On Fri, Sep 23, 2011 at 1:45 PM, R. Michael Weylandt
michael.weyla...@gmail.com wrote:
What exactly
assign() doesn't return anything in this case. It's your addtional
(unnecessary?) call to zzz at the end which triggers a print statement.
Michael
On Fri, Sep 23, 2011 at 1:56 PM, Jim Bouldin bouldi...@gmail.com wrote:
OK, I see. I thought R was just returning the character strings of the
OK. I was assuming that the call to zzz would print the model formulae, not
the object names. That's what threw me.
Jim
On Fri, Sep 23, 2011 at 1:59 PM, R. Michael Weylandt
michael.weyla...@gmail.com wrote:
assign() doesn't return anything in this case. It's your addtional
(unnecessary?) call
Two things:
I think you are not aware of the R difference between a formula and a lm
object. A formula is part of the input to the lm function while the output
is a complicated object of lm class. If you want the formulas back from the
model object, you can access them by way of
model1$call with
9 matches
Mail list logo