Skip to content
Prev 280758 / 398503 Next

Renaming Within A Function

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.
+  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
+ }
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:
+  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)
+ }
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: