Jim Bouldin
2011-Sep-23 17:03 UTC
[R] converting object elements to variable names and making subsequent assignments thereto
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]) and lm(y~x[,2]), to the variables "model1" and "model2". There are of course, many more than 2 models involved, so brute force is the option of absolute last resort. Thanks for any help. -- Jim Bouldin, Research Ecologist [[alternative HTML version deleted]]
R. Michael Weylandt
2011-Sep-23 17:08 UTC
[R] converting object elements to variable names and making subsequent assignments thereto
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 <bouldinjr@gmail.com> wrote:> 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]) > and > lm(y~x[,2]), to the variables "model1" and "model2". > > There are of course, many more than 2 models involved, so brute force is > the > option of absolute last resort. > Thanks for any help. > -- > Jim Bouldin, Research Ecologist > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Jean V Adams
2011-Sep-23 17:36 UTC
[R] converting object elements to variable names and making subsequent assignments thereto
Jim Bouldin wrote on 09/23/2011 12:03:47 PM:> > This has got to be incredibly simple but I nevertheless can't figure itout> as I am apparently brain dead. > > I just want to convert the elements of a character vector to variablenames,> 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])and> lm(y~x[,2]), to the variables "model1" and "model2". > > There are of course, many more than 2 models involved, so brute force isthe> option of absolute last resort. > Thanks for any help. > -- > Jim Bouldin, Research EcologistIt's not clear to me what you're trying to do. You say you want to assign formulae, such as lm(y~x[,1]) and lm(y~x[,2]), to the variables "model1" and "model2". Do you mean that you want the resulting formulae to be lm(y~model1) and lm(y~model2)? Could you give a more complete example of what you have and what you'd like to end up with? Jean [[alternative HTML version deleted]]