[R] Renaming Within A Function

Pete Brecknock Peter.Brecknock at bp.com
Thu Dec 22 20:15:23 CET 2011


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.



More information about the R-help mailing list