Rollapply:by.column 有什么作用? [英] Rollapply: what does by.column do?
问题描述
我已经阅读了手册中对 rollapply
的 by.column
的描述,但我不明白如何使用它.见下文:
x=matrix(1:60,nrow=10)图书馆('动物园')rollapply(x,3,mean,fill=NA,align="right",by.column=FALSE)[1] 不适用 不适用 27 28 29 30 31 32 33 34
当我使用 by.column= FALSE
时:它适用于宽度 (3) 滚动行数 mean(x[1:3,])
>
现在,如果我使用 by.column=TRUE
然后我得到:
x=matrix(1:60,nrow=10)rollapply(x,3,mean,fill=NA,align="right",by.column=TRUE)[,1] [,2] [,3] [,4] [,5] [,6][1,] 呐呐呐呐呐呐呐呐[2,] 呐呐呐呐呐呐呐呐[3,] 2 12 22 32 42 52[4,] 3 13 23 33 43 53[5,] 4 14 24 34 44 54[6,] 5 15 25 35 45 55[7,] 6 16 26 36 46 56[8,] 7 17 27 37 47 57[9,] 8 18 28 38 48 58[10,] 9 19 29 39 49 59
我无法理解结果.谁能解释一下 by.column
的用途,并提供一个例子?
by.column = TRUE
(这是默认值) with FUN = mean
做滚动平均分别为每一列.结果的第 i 列将是:
rollapplyr(x[, i], 3, mean, fill = NA)
by.column = FALSE
一次将所有列输入到函数中,因此在这种情况下它与:
c(NA, NA, sapply(1:8, function(ix) mean(x[seq(ix, ix+2), ])))
I have read the description of by.column
for rollapply
in the manual but I couldn't understand how to use it. see below:
x=matrix(1:60,nrow=10)
library('zoo')
rollapply(x,3,mean,fill=NA,align="right",by.column=FALSE)
[1] NA NA 27 28 29 30 31 32 33 34
when i use by.column= FALSE
: it applies mean to width (3) rolling number of lines mean(x[1:3,])
now, if I use by.column=TRUE
then I get:
x=matrix(1:60,nrow=10)
rollapply(x,3,mean,fill=NA,align="right",by.column=TRUE)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] NA NA NA NA NA NA
[2,] NA NA NA NA NA NA
[3,] 2 12 22 32 42 52
[4,] 3 13 23 33 43 53
[5,] 4 14 24 34 44 54
[6,] 5 15 25 35 45 55
[7,] 6 16 26 36 46 56
[8,] 7 17 27 37 47 57
[9,] 8 18 28 38 48 58
[10,] 9 19 29 39 49 59
I can't make sense of the result. could anyone please explain what's the use of by.column
and maybe provide an example?
by.column = TRUE
(which is the default) with FUN = mean
does a rolling mean separately for each column. The ith column of the result would be:
rollapplyr(x[, i], 3, mean, fill = NA)
by.column = FALSE
inputs all columns at once to the function so in this case it would be the same as:
c(NA, NA, sapply(1:8, function(ix) mean(x[seq(ix, ix+2), ])))
这篇关于Rollapply:by.column 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!