我在R语言的学习方面取得了长足的进步,到目前为止,我对环境感到很满意。我发现R是一种解释器语言,而不是像我习惯的VBA这样的 compiler 语言。但是,我认为R中一定有创建程序的方法,而最接近的是“函数”。这是我现在正在做的事情-
Daily <- subset(InputData,ï..Frequency == "Daily") #Break apart the data set by frequency
Daily$ï..Frequency <- NULL #Remove the Frequency Column
Daily$Date <- as.Date(Daily$Date,format = '%d/%m/%Y')
Weekly <- subset(InputData,ï..Frequency == "Weekly")
Weekly$ï..Frequency <- NULL
Weekly$Date <- as.Date(Weekly$Date,format = '%d/%m/%Y')
Monthly <- subset(InputData,ï..Frequency == "Monthly")
Monthly$ï..Frequency <- NULL
Monthly$Date <- as.Date(Monthly$Date,format = '%d/%m/%Y')
Quarterly <- subset(InputData,ï..Frequency == "Quarterly")
Quarterly$ï..Frequency <- NULL
Quarterly$Date <- as.Date(Quarterly$Date,format = '%d/%m/%Y')
Yearly <- subset(InputData,ï..Frequency == "Yearly")
Yearly$ï..Frequency <- NULL
Yearly$Date <- as.Date(Yearly$Date,format = '%d/%m/%Y')
可以看出,我正在基于输入数组中的字符串变量创建多个数据帧,该数据帧可以采用五个值-“每日”,“每周”,“每月”,“每季度”和“每年” “。之后,我将在这些子集中添加新的列
Daily$ETF.Return <- as.numeric(c("-",diff(log(Daily$ETF.Index)))) #Calculate ETF Return
Daily$Index.Return <- as.numeric(c("-",diff(log(Daily$Underlying.Index)))) #Calculate Index Return
Daily$Multiple <- Daily$ETF.Return/Daily$Index.Return #Calculate Multiple
依此类推。一旦创建了这些新数据框,我将对其进行一些统计分析和回归绘图,我想这并不难。但是问题是,我无法将字符串用作UDF的输入,并且随后无法使用input参数将新列添加到新数据帧。我该怎么做呢?此外,是否有可能在R中创建一个过程,类似于可以调用和触发的VBA中的子例程?我将不胜感激所有反馈! 这是一些示例数据,我猜测缺少这些数据会使事情变得模棱两可-