Rollapply: what does by.column do?

删除回忆录丶 提交于 2020-03-06 02:27:52

问题


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?


回答1:


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), ])))


来源:https://stackoverflow.com/questions/32951459/rollapply-what-does-by-column-do

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!