Hi, if I know the colnames x and y in the following example, I can easily to do lm. tmp <- data.frame(x=c(1,1.2),y=c(1,2)) lm(y ~ x, data=tmp) when the colnames are variable, what should I do? for example colnames(tmp)[1] <- paste("aa",1,sep="_") lm(y ~ paste("aa",1,sep="_"), data = tmp) it gives me error. [[alternative HTML version deleted]]
On 21.05.2010 09:54, Yuan Jian wrote:> Hi, > > if I know the colnames x and y in the following example, I can easily to do lm. > tmp<- data.frame(x=c(1,1.2),y=c(1,2)) > lm(y ~ x, data=tmp) > > when the colnames are variable, what should I do? for example > colnames(tmp)[1]<- paste("aa",1,sep="_") > lm(y ~ paste("aa",1,sep="_"), data = tmp)myformula <- formula(paste("y ~", paste("aa", 1, sep="_"))) lm(myformula, data = tmp) Uwe Ligges> it gives me error. > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Hi! The problem is that paste() returns a character string. You can use get() this way: lm(y ~ get(paste("aa",1,sep="_")), data = tmp) Or: lm(tmp[[2]]~tmp[[1]], data=tmp) HTH, Ivan Le 5/21/2010 09:54, Yuan Jian a ?crit :> Hi, > > if I know the colnames x and y in the following example, I can easily to do lm. > tmp<- data.frame(x=c(1,1.2),y=c(1,2)) > lm(y ~ x, data=tmp) > > when the colnames are variable, what should I do? for example > colnames(tmp)[1]<- paste("aa",1,sep="_") > lm(y ~ paste("aa",1,sep="_"), data = tmp) > it gives me error. > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
Try this: lm(y ~., tmp) On Fri, May 21, 2010 at 3:54 AM, Yuan Jian <jayuan2008 at yahoo.com> wrote:> Hi, > > if I know the colnames x and y in the following example, I can easily to do lm. > tmp <- data.frame(x=c(1,1.2),y=c(1,2)) > lm(y ~ x, data=tmp) > > when the colnames are variable, what should I do? for example > colnames(tmp)[1] <- paste("aa",1,sep="_") > lm(y ~ paste("aa",1,sep="_"), data = tmp) > it gives me error. > > > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >