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 +0000 (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.

Reply via email to