我有一个" mydata"数据框为2列:日期列和值列。我想在数据框中附加列,这些列是"值"的移动平均值。不同窗口的专栏。
目前mydata有2列
Column 1 - date
Column 2 - value
我希望第3列是值列的2周期移动平均值,第4列是值列的3周期移动平均值等。直到最后一列是9个周期的移动平均值
所以我想添加这些列:
Column 3 - SMA(value, 2)
Column 4 - SMA(value, 3)
Column 5 - SMA(value, 4)
Column 6 - SMA(value, 5)
Column 7 - SMA(value, 6)
Column 8 - SMA(value, 7)
Column 9 - SMA(value, 8)
Column 10 - SMA(value, 9)
这是代码
library(TTR)
date = seq(as.Date("2016-01-01"),as.Date("2016-01-10"),"day")
value =c(1,2,3,4,5,6,7,8,9,10)
mydata = data.frame (date, value)
z = zoo(value, date)
SMA(z, n=2)# this would be 3rd column of data frame
SMA(z, n=3)# this would be 4th column of data frame
等.....
谢谢。答案 0 :(得分:4)
您可以使用data.table
软件包以及rollmeanr
软件包中的rollmean
函数(或zoo
函数)实现此目的:
library(data.table)
library(zoo)
# method 1:
setDT(mydata)[, paste0('MA',2:9) := lapply(2:9, function(x) rollmeanr(value, x, fill = NA))][]
# method 2:
setDT(mydata)[, paste0('MA',2:9) := lapply(2:9, rollapplyr, data = value, mean, fill = NA)][]
两者都给出了:
> mydata
date value MA2 MA3 MA4 MA5 MA6 MA7 MA8 MA9
1: 2016-01-01 1 NA NA NA NA NA NA NA NA
2: 2016-01-02 2 1.5 NA NA NA NA NA NA NA
3: 2016-01-03 3 2.5 2 NA NA NA NA NA NA
4: 2016-01-04 4 3.5 3 2.5 NA NA NA NA NA
5: 2016-01-05 5 4.5 4 3.5 3 NA NA NA NA
6: 2016-01-06 6 5.5 5 4.5 4 3.5 NA NA NA
7: 2016-01-07 7 6.5 6 5.5 5 4.5 4 NA NA
8: 2016-01-08 8 7.5 7 6.5 6 5.5 5 4.5 NA
9: 2016-01-09 9 8.5 8 7.5 7 6.5 6 5.5 5
10: 2016-01-10 10 9.5 9 8.5 8 7.5 7 6.5 6
在回复您的评论时,您可以通过链接类似的操作来添加log
值:
setDT(mydata)[, paste0('MA',2:9) := lapply(2:9, function(x) rollmeanr(value, x, fill = NA))
][, paste0('ln',2:9) := lapply(.SD, log), .SDcols = 3:10]
给出:
> mydata
date value MA2 MA3 MA4 MA5 MA6 MA7 MA8 MA9 ln2 ln3 ln4 ln5 ln6 ln7 ln8 ln9
1: 2016-01-01 1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
2: 2016-01-02 2 1.5 NA NA NA NA NA NA NA 0.4054651 NA NA NA NA NA NA NA
3: 2016-01-03 3 2.5 2 NA NA NA NA NA NA 0.9162907 0.6931472 NA NA NA NA NA NA
4: 2016-01-04 4 3.5 3 2.5 NA NA NA NA NA 1.2527630 1.0986123 0.9162907 NA NA NA NA NA
5: 2016-01-05 5 4.5 4 3.5 3 NA NA NA NA 1.5040774 1.3862944 1.2527630 1.098612 NA NA NA NA
6: 2016-01-06 6 5.5 5 4.5 4 3.5 NA NA NA 1.7047481 1.6094379 1.5040774 1.386294 1.252763 NA NA NA
7: 2016-01-07 7 6.5 6 5.5 5 4.5 4 NA NA 1.8718022 1.7917595 1.7047481 1.609438 1.504077 1.386294 NA NA
8: 2016-01-08 8 7.5 7 6.5 6 5.5 5 4.5 NA 2.0149030 1.9459101 1.8718022 1.791759 1.704748 1.609438 1.504077 NA
9: 2016-01-09 9 8.5 8 7.5 7 6.5 6 5.5 5 2.1400662 2.0794415 2.0149030 1.945910 1.871802 1.791759 1.704748 1.609438
10: 2016-01-10 10 9.5 9 8.5 8 7.5 7 6.5 6 2.2512918 2.1972246 2.1400662 2.079442 2.014903 1.945910 1.871802 1.791759