[R] using get() in assign()

Spencer Graves spencer.graves at pdf.com
Wed Dec 29 05:47:45 CET 2004


      Consider the following: 

 > changeNames <- function(dfName,
+          newNames=c("A1", "B1"), pos.=1){
+   DF. <- get(dfName)
+   names(DF.) <- newNames
+   assign(dfName, DF., pos=pos.)
+   "done"
+ }
 >
 > DF <- data.frame(a=1, b=2)
 > changeNames("DF")
[1] "done"
 > DF
  A1 B1
1  1  2
 >
      There's probably a way to do this with "do.call", but the above 
seems to work. 

      hope this helps.  spencer graves

thomas hills wrote:

>I'm trying to rename the columns in a list of data.frames using the 
>following...
>
>for(i in 1:length(filenames)) {
>assign(names(get(filenames[i])), c("name", "infood", "time") ) }
>
>R returns no errors, but the names are unchanged in the data.frames.
>
>The original names were things like
>
> > names(get(filenames[2]))
>[1] "Tc45w4.V1" "Tc45w4.V2" "Tc45w4.V3"
>
>after the above procedure they are still those names.
>
>Ideas appreciated.  Thanks.
>
>Thomas
>
>
>
>	[[alternative text/enriched 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
>  
>

-- 
Spencer Graves, PhD, Senior Development Engineer
O:  (408)938-4420;  mobile:  (408)655-4567




More information about the R-help mailing list