Skip to content
Prev 171254 / 398506 Next

how to add names to an object created using assign

At this point, the function would only return the names because unless  
specified otherwise they return the result of the last evaluation. Add  
another line with just "a" and assign the results of the function to  
something:

 > testold=function(){
+ assign("a",c(1,2,3),env=.GlobalEnv)
+ names(a)=c("one","two","three")
+ }
 > a3 <- testold()
 > a3
[1] "one"   "two"   "three"


test=function(){
  assign("a",c(1,2,3),env=.GlobalEnv)  # I rather doubt the env  
assignment is needed
  names(a)=c("one","two","three");
  a
  }
  a2 <- test()
-----
 > a2
   one   two three
     1     2     3

QED;
David Winsemius
On Feb 20, 2009, at 6:32 PM, Fuchs Ira wrote: