滚动应用中的对齐方式和偏移量 [英] alignment and offsets in rollapply
问题描述
我正在尝试计算移动窗口的一些统计信息,并在Zoo软件包中使用rollapply.我的问题是我如何滚动应用该函数以将其应用到先前的n个观测值,而不是当前的观测值和先前的n-1个观测值,就像右对齐一样.
I am trying to calculate some statistics for a moving window and am using rollapply in the zoo package. My question is how do I get rollapply to apply that function to the previous n observations instead of the current observation and the previous n-1 observations as align right seems to do.
require(zoo)
z <- data.frame(x1=11:111, x2=111:211, x3=as.Date(31:131))#generate data
output<-data.frame(dates=z$x3,
rollapply(z[,1:2],by.column=TRUE, 5, max, fill=NA, align='right'))
我有一个预感,可以用?rollapply来回答:如果width是一个纯数字矢量,则将其元素视为与align一起解释的宽度,而如果width是一个列表,则将其分量视为偏移量.在上面例如,如果width的长度为1,则将按每个by-by点循环使用width.如果width是一个列表,则其分量表示整数偏移量,以使列表的第i个分量表示位置i + width [[一世]]."但是我不知道这对于R代码意味着什么,没有提供示例.
I have a hunch this is answered by ?rollapply "If width is a plain numeric vector its elements are regarded as widths to be interpreted in conjunction with align whereas if width is a list its components are regarded as offsets. In the above cases if the length of width is 1 then width is recycled for every by-th point. If width is a list its components represent integer offsets such that the i-th component of the list refers to time points at positions i + width[[i]]." But I have no idea what that means in terms of R code an no example is provided.
推荐答案
没关系,我破译了帮助".像这样将参数width
添加到rollapply
:
Nevermind, I deciphered the 'help.' Adding the parameter width
to rollapply
like this:
width=list(-1:-5)
完成它.
这篇关于滚动应用中的对齐方式和偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!