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.
Why do you want to create a variable within the function since it will disappear after the return. Is there a reason for that? Here is one way of getting the names by just using them on the cbind. Also you are create a 'matrix' and 'names' is not appropriate for that type of object.> # 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(Dates = dts, Data = d)) + get(paste(var,".df",sep="")) # return object + }> > # Call Function > myfunc("X")Dates Data [1,] 14975 1 [2,] 15006 2 [3,] 15034 3 [4,] 15065 4 [5,] 15095 5 To get the same return value, you could:> # 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)) + + cbind(Dates = dts, Data = d) + }> > # Call Function > myfunc("X")Dates Data [1,] 14975 1 [2,] 15006 2 [3,] 15034 3 [4,] 15065 4 [5,] 15095 5>On Thu, Dec 22, 2011 at 2:15 PM, Pete Brecknock <Peter.Brecknock at bp.com> wrote:> 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 at 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
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.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Pete Brecknock > Sent: Thursday, December 22, 2011 12:15 PM > To: r-help at 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 at 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.