[R] fMultivar rollMax question

Diethelm Wuertz wuertz at itp.phys.ethz.ch
Tue Oct 3 00:29:45 CEST 2006


Omar Lakkis wrote:

>I am using fMultivar under R 2.2.1 on a Debian linux box. Could
>someone, please, explain to me why there are two trailing NAs in the
>last statement in the code beow?
>
>
>  
>
>>library(fMultivar)
>>x <- 1:20
>>rollMax(x, n=3)
>>    
>>
> [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
>  
>
>>rollMax(x, n=2)
>>    
>>
> [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
>  
>
>>rollMax(x, n=1)
>>    
>>
> [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 NA NA
>  
>

The rolling functions were build for n > 1, which is unfortunately not 
mentioned in the
help page. For n=1 you can just call: apply(x, 1, max)

If we want rolling functions which also work for n=1 we have to modify the
code in the following way:

replace in the function rollFun() the following lines

    for (i in 2:n) {
       start = start + 1
       end = end + 1
        m = cbind(m, x[start:end])
   }

with

    if (n > 1) {
        for (i in 2:n) {
            start = start + 1
            end = end + 1
            m = cbind(m, x[start:end])
        }
    } else if (n == 1) {
        m = matrix(m)
    }


Regards DW



>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>  
>



More information about the R-help mailing list