[R] Re : change the names of all listed objects

Duncan Murdoch murdoch at stats.uwo.ca
Fri Aug 3 17:46:44 CEST 2007


On 8/3/2007 11:28 AM, justin bem wrote:
> It not too simple what ls return the variable that have been create in your environment even if you change their names, ls will not return "a1" "b1" ...
> 
> The names attribute in OOP of an object is just an attribute and it is different with an instance of the object. ls() show instance
> 
> But you can try this :
> 
> a<-10
>> ls()
> [1] "a"
> 
> assign(paste(deparse(substitute(a)),"1",sep=""),a)                  # here I create a new instance with name                                                                                             # a1 and the same value than a
> rm(a)                                                                                    # In destroy the original.

The use of deparse(substitute(a)) suggests you want to do this in a 
function, but then your rm() will only remove the local copy.  Writing a 
gemeral purpose function for this is tricky, because it's not obvious 
what should happen in certain weird cases.  For example:

rename( c, collect )

to rename the c() function to collect():  should it really try to go 
into the base environment and delete the c object?  That would be fairly 
disastrous if it succeeded.  But if it found a c() function in the 
global environment, removing it from there might be sensible (maybe even 
if this rename request were executed from a function, rather than at the 
top level).

Duncan Murdoch

> 
>>ls()
> [1] "a1"
> 
> 
>  
> Justin BEM
> BP 1917 Yaoundé
> Tél (237) 99597295
>     (237) 22040246
> 
> ----- Message d'origine ----
> De : Luca Laghi <l.laghi at unibo.it>
> À : r-help at stat.math.ethz.ch
> Envoyé le : Vendredi, 3 Août 2007, 15h23mn 01s
> Objet : [R] change the names of all listed objects
> 
> Dear list members,
> I have created seven objects, from a to g, so that ls() gives me
> [1] "a" "b" "c" "d" "e" "f" "g"
> 
> how can I automatically change their names in
> 
> "a1" "b1" "c1" "d1" "e1" "f1" "g1"
> 
> It seems simple, ut it is driving me mad.
> Thank you very much for your help.
> Luca Laghi
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 
> 
> 
> 
> 
> 
> 
>       _____________________________________________________________________________ 
> 
> l 
> 	[[alternative HTML version deleted]]
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.



More information about the R-help mailing list