Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible URL : <https://stat.ethz.ch/pipermail/r-help/attachments/20120318/d68edf35/attachment.pl>
Do not use assign(). It is a relic from the 1980s. Instead, decide where you want your variables to live, perhaps in a list, where<-list() or perhaps in an environment, where<-new.env() or where<-environment(). Then use where[[varName]] to refer to the variable. You can use further subsetting functions on that. E.g., where <- environment() # the current environment varName <- "qwerty" where[[varName]] <- 1:10 where[[varName]][2:3] <- log(where[[varName]][9:10]) where[[varName]] # [1] 1.000000 2.197225 2.302585 4.000000 # [5] 5.000000 6.000000 7.000000 8.000000 # [9] 9.000000 10.000000 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Marc Girondot > Sent: Sunday, March 18, 2012 11:25 AM > To: r-help at r-project.org > Subject: [R] assign a value to an element > > Assign can be used to set a value to a variable that has name as a value of another > variable. Example: > > > name<-"essai" > > assign(name, "plouf") > > essai > [1] "plouf" > > OK. > But how to do the same when it is only an element of a vector, data frame and so on > that must be changed. > > > vec<-1:10 > > vec > ?[1] ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 10 > > vec[4] > [1] 4 > > name<-"vec[4]" > > assign(name, 100) > > vec > ?[1] ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 10 > > The reason is probably here (from help of assign): > assign?does not dispatch assignment methods, so it cannot be used to set elements of > vectors, names, attributes, etc. > > > I have found this solution: > > eval(parse(text=paste(name, "<-100", sep=""))) > > vec > ?[1] ? 1 ? 2 ? 3 100 ? 5 ? 6 ? 7 ? 8 ? 9 ?10 > > Is-it the only way ? It is not very elegant ! > > Thanks a lot > > Marc > > __________________________________________________________ > Marc Girondot, Pr > > Laboratoire Ecologie, Syst?matique et Evolution > Equipe de Conservation des Populations et des Communaut?s > CNRS, AgroParisTech et Universit? Paris-Sud 11 , UMR 8079 > B?timent 362 > 91405 Orsay Cedex, France > > Tel:? 33 1 (0)1.69.15.72.30?? Fax: 33 1 (0)1.69.15.73.53 > e-mail: marc.girondot at u-psud.fr > Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html > Skype: girondot > [[alternative HTML version deleted]]
On Mar 18, 2012, at 2:24 PM, Marc Girondot wrote:> Assign can be used to set a value to a variable that has name as a > value of another variable. Example: > >> name<-"essai" >> assign(name, "plouf") >> essai > [1] "plouf" > > OK. > But how to do the same when it is only an element of a vector, data > frame and so on that must be changed. > >> vec<-1:10 >> vec > [1] 1 2 3 4 5 6 7 8 9 10 >> vec[4] > [1] 4 >> name<-"vec[4]" >> assign(name, 100) >> vec > [1] 1 2 3 4 5 6 7 8 9 10 > > The reason is probably here (from help of assign): > assign does not dispatch assignment methods, so it cannot be used to > set elements of vectors, names, attributes, etc.Yes and further, vec[4] in your example does not have a name, since you created vect as an unnamed vector. It's not generally optimal practice to build up strings and pass them to eval(parse()). However there is some assignment possible by name to data.frame rows with "[" > vecdf <- data.frame(vec=vec) > vecdf['a' , ] <- 20 > vecdf vec a 20 b 10 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 Where the rowname value is used as the index for assignment. Is that sufficiently elegant?> > I have found this solution: >> eval(parse(text=paste(name, "<-100", sep=""))) >> vec > [1] 1 2 3 100 5 6 7 8 9 10 > > Is-it the only way ? It is not very elegant ! > > Thanks a lot > > MarcDavid Winsemius, MD West Hartford, CT
I am not sure that I understand but does something like this do what you want? ec<-1:10 vec[vec==4] <- 100 vec <- 1:10 vec[ vec==4 | vec==8] <- 100 vec <- 1:10 aa <- 50 vec[vec==4] <- aa John Kane Kingston ON Canada> -----Original Message----- > From: marc_grt at yahoo.fr > Sent: Sun, 18 Mar 2012 18:24:34 +0000 (GMT) > To: r-help at r-project.org > Subject: [R] assign a value to an element > > Assign can be used to set a value to a variable that has name as a value > of another variable. Example: > >> name<-"essai" >> assign(name, "plouf") >> essai > [1] "plouf" > > OK. > But how to do the same when it is only an element of a vector, data frame > and so on that must be changed. > >> vec<-1:10 >> vec > [1] 1 2 3 4 5 6 7 8 9 10 >> vec[4] > [1] 4 >> name<-"vec[4]" >> assign(name, 100) >> vec > [1] 1 2 3 4 5 6 7 8 9 10 > > The reason is probably here (from help of assign): > assign does not dispatch assignment methods, so it cannot be used to set > elements of vectors, names, attributes, etc. > > > I have found this solution: >> eval(parse(text=paste(name, "<-100", sep=""))) >> vec > [1] 1 2 3 100 5 6 7 8 9 10 > > Is-it the only way ? It is not very elegant ! > > Thanks a lot > > Marc > > __________________________________________________________ > Marc Girondot, Pr > > Laboratoire Ecologie, Systimatique et Evolution > Equipe de Conservation des Populations et des Communautis > CNRS, AgroParisTech et Universiti Paris-Sud 11 , UMR 8079 > Bbtiment 362 > 91405 Orsay Cedex, France > > Tel: 33 1 (0)1.69.15.72.30 Fax: 33 1 (0)1.69.15.73.53 > e-mail: marc.girondot at u-psud.fr > Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!