Good Evening. I have code that requires creating new variable names. I am presently using the assign function. After I create my new desired data structure, i am having difficulty modifying it thereafter. I have read many times that storing the variable names in lists is the ideal process, but i am having trouble getting there.
This code simply attempts to add a year column to the NewVar xts object.
The larger scope would be to loop through multiple years and 2018 would be ultimately replaced by i.
Thank you!
xts1 <- xts(x=1:10, order.by=Sys.Date()-1:10)
xts2 <- xts(x=21:30, order.by=Sys.Date()-1:10)
NewName = "NewVar"
assign(NewName, xts1 - xts2)
assign(NewName$year, 2018) # this is the code that fails logically...
EDIT:
The desired outcome is to have the NewVar XTS object, a time series of 10 values, have an additional column that displays 2018 for each value.
Yes, I can just say NewVar$year <- 2018. But i need to access the assigned variable name within the global environment (NewVar), without explicity typing it. Is there a way to access the XTS Object NewVar using NewName, and then modify it? My method finds the vector, rather than the newly created XTS. THANKS!
Why don't use NewName["year"]=2018 directly?
Edits: OK, not quite sure I get it. See if this works for you.
xts1 <- xts(x=1:10, order.by=Sys.Date()-1:10)
xts2 <- xts(x=21:30, order.by=Sys.Date()-1:10)
NewName = "NewVar"
assign(NewName, xts(cbind(N=xts1 - xts2, year=2018)))