简体   繁体   中英

Rollapply: what does by.column do?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM