-2

我必须使用此函数计算某些企业的月收益:

第i+1月的月收益=(第i+1月的收盘价-第i月的收盘价)/第i月的收盘价

我有一个数据集,其中包含大约 10 年的每个月的收盘价。我将如何创建/使用函数来计算每个月的 MR?

4

1 回答 1

0
monthly <- c(100, 120, 130, 100, 140)  # replace this of course with actual data

result <- rep(NA, length(monthly))
for (i in 1:(length(monthly)-1)) {
   result[i+1] <- (monthly[i+1] - monthly[i]) / monthly[i] 
}

result现在将包含您的答案。请注意,第一个位置将包含一个NA,因为您对月份i为 1 不感兴趣。

于 2013-10-27T23:09:00.937 回答