Re: [R] assign a value to an element
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_...@yahoo.fr Sent: Sun, 18 Mar 2012 18:24:34 + (GMT) To: r-help@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.giron...@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! __ 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.
[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.giron...@u-psud.fr Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html Skype: girondot [[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.
Re: [R] assign a value to an element
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.00 2.197225 2.302585 4.00 # [5] 5.00 6.00 7.00 8.00 # [9] 9.00 10.00 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Marc Girondot Sent: Sunday, March 18, 2012 11:25 AM To: r-help@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.giron...@u-psud.fr Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html Skype: girondot [[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.
Re: [R] assign a value to an element
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 Marc David Winsemius, MD West Hartford, CT __ 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.