0

How can I refer to the choice of a function's argument in the course of my further coding?---A specific example:

library("quantmod")

INDEX<-get(getSymbols("^GDAXI", from="2006-01-01"))
INDEX.SMA<-SMA(INDEX[,4],n=360)

INDEX<-INDEX[,4]
colnames(INDEX)<-c("Close")
colnames(INDEX.SMA)<-"360"

The function SMA(INDEX,n=360) generates the moving average of closing prices in INDEX.

I would like to have the chosen argument of 360 be automatically reflected in colnames(INDEX.SMA). Thus, I don't want to change it manually to, say, "200", four lines further down after changing my code to

INDEX.SMA<-SMA(INDEX[,4],n=200)

Replacing

colnames(INDEX.SMA)<-"360"

with

colnames(INDEX.SMA)<-as.character(length(INDEX.SMA)-sum(!is.na(INDEX.SMA))+1)

did the job in this specific example. Is there a more general solution?

Clyde Frog
  • 483
  • 1
  • 6
  • 15

1 Answers1

1

If you need to use a value in multiple places, you should make it a variable

library("quantmod")

mytime<-360

INDEX<-get(getSymbols("^GDAXI", from="2006-01-01"))
INDEX.SMA<-SMA(INDEX[,4],n=mytime)

INDEX<-INDEX[,4]
colnames(INDEX)<-c("Close")
colnames(INDEX.SMA)<-as.character(mytime)
MrFlick
  • 195,160
  • 17
  • 277
  • 295