Hi all, I'm using R 1.5.1 on Windows 2000. The following snippet of code doesn't seem to do anything - no error is reported, and there is no name change. names(myFrame[,c(1:3)]) <- c("name1", "name2", "name3") This code however works nicely: names(myFrame)[c(1:3)] <- c("name1", "name2", "name3") Can anyone suggest why the first doesn't work? Ought an error be reported? Thanks, Andrew Andrew Robinson Phone: 208-885-7115 Department of Forest Resources Fax: 208-885-6226 University of Idaho E: andrewr at uidaho.edu Po Box 441133 WWW: http://www.uidaho.edu/~andrewr Moscow, ID 83843 and: http://www.biometrics.uidaho.edu/ No statement above may be construed to necessarily represent my employer. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 17 Jul 2002, Andrew Robinson wrote:> Hi all, > > I'm using R 1.5.1 on Windows 2000. The following snippet of code doesn't > seem to do anything - no error is reported, and there is no name change. > > names(myFrame[,c(1:3)]) <- c("name1", "name2", "name3") > > This code however works nicely: > > names(myFrame)[c(1:3)] <- c("name1", "name2", "name3") > > Can anyone suggest why the first doesn't work? Ought an error be reported?It does work. It creates a new object myFrame[,c(1:3)] and changes its names. That's not what you wanted, but R is your servant not your master and does allow you to do such things. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
At 08:01 AM 7/18/2002 +0100, Brian D. Ripley wrote:>On Wed, 17 Jul 2002, Andrew Robinson wrote: > > > Hi all, > > > > I'm using R 1.5.1 on Windows 2000. The following snippet of code doesn't > > seem to do anything - no error is reported, and there is no name change. > > > > names(myFrame[,c(1:3)]) <- c("name1", "name2", "name3") > > > > This code however works nicely: > > > > names(myFrame)[c(1:3)] <- c("name1", "name2", "name3") > > > > Can anyone suggest why the first doesn't work? Ought an error be reported? > >It does work. It creates a new object myFrame[,c(1:3)] and changes its >names. That's not what you wanted, but R is your servant not your master >and does allow you to do such things.I'm sorry but I'm still confused. Do you mean that another object called myFrame is created, with the new names? It doesn't appear in the objects() list. The original is still there, within unchanged names. Andrew Andrew Robinson Phone: 208-885-7115 Department of Forest Resources Fax: 208-885-6226 University of Idaho E: andrewr at uidaho.edu Po Box 441133 WWW: http://www.uidaho.edu/~andrewr Moscow, ID 83843 and: http://www.biometrics.uidaho.edu/ No statement above may be construed to necessarily represent my employer. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
You asked R to create a new data frame MyFrame[,1:3] (note: you don't need the c()), then change the names of this data frame. Since you didn't ask R to store this in an object, it gets discarded. Constructions such as names(MyFrame[,1:3]) <- something simply won't work as you expected, and even if it worked, would be much less efficient than names(MyFrame)[1:3] <- something because you are simply assigning names to some columns. There's no need to ask R to extract those columns out of the data frame. Andy> -----Original Message----- > From: Andrew Robinson [mailto:andrewr at uidaho.edu] > Sent: Thursday, July 18, 2002 12:07 PM > To: ripley at stats.ox.ac.uk > Cc: r-help at stat.math.ethz.ch; Robert Keefe > Subject: Re: [R] Oddity with names > > > At 08:01 AM 7/18/2002 +0100, Brian D. Ripley wrote: > >On Wed, 17 Jul 2002, Andrew Robinson wrote: > > > > > Hi all, > > > > > > I'm using R 1.5.1 on Windows 2000. The following snippet > of code doesn't > > > seem to do anything - no error is reported, and there is > no name change. > > > > > > names(myFrame[,c(1:3)]) <- c("name1", "name2", "name3") > > > > > > This code however works nicely: > > > > > > names(myFrame)[c(1:3)] <- c("name1", "name2", "name3") > > > > > > Can anyone suggest why the first doesn't work? Ought an > error be reported? > > > >It does work. It creates a new object myFrame[,c(1:3)] and > changes its > >names. That's not what you wanted, but R is your servant > not your master > >and does allow you to do such things. > > I'm sorry but I'm still confused. Do you mean that another > object called > myFrame is created, with the new names? It doesn't appear in > the objects() > list. The original is still there, within unchanged names. > > Andrew > > Andrew Robinson Phone: 208-885-7115 > Department of Forest Resources Fax: 208-885-6226 > University of Idaho E: andrewr at uidaho.edu > Po Box 441133 WWW: http://www.uidaho.edu/~andrewr > Moscow, ID 83843 and:http://www.biometrics.uidaho.edu/ No statement above may be construed to necessarily represent my employer. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it. ============================================================================= -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._