The error is because you are trying to assign to the result of a get call, and 
nobody has programmed that (hence "could not find function") because it is 
mostly (if not completely) meaningless to do so.

It is not completely clear what you want to accomplish, but there is probably a 
better way to accomplish it.  Preferable is to create and modify the data 
object fully within the function then return that object (and let the caller of 
the function worry about assigning it).

Some things to read that may be enlightening if you really feel the need to 
have your "function" modify existing objects:

> library(fortunes)
> fortune(236)

And 

http://cran.r-project.org/doc/Rnews/Rnews_2001-3.pdf
(the article in the "Programmer's Niche")

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Pete Brecknock
> Sent: Thursday, December 22, 2011 12:15 PM
> To: r-help@r-project.org
> Subject: [R] Renaming Within A Function
> 
> I am trying to rename column names in a dataframe within a function. I
> am
> seeing an error (listed below) that I don't understand.
> 
> Would be grateful of an explanation of what I am doing wrong and how I
> should rewrite the function to allow me to be able to rename my
> variables.
> 
> Thanks.
> 
> # Test Function
> myfunc <-function(var){
>   d   = c(1,2,3,4,5)
>   dts = seq(as.Date("2011-01-01"),by="month",length.out=length(d))
> 
>   assign(paste(var,".df",sep=""), cbind(dts, d))
> 
>   names(get(paste(var,".df",sep="")))  <- c("Dates","Data")
> }
> 
> # Call Function
> myfunc("X")
> 
> # Error Message
> Error in names(get(paste(var, ".df", sep = ""))) <- c("Dates", "Data")
> :
>   could not find function "get<-"
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Renaming-
> Within-A-Function-tp4226368p4226368.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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-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