[R] averageif and looping

Rui Barradas ruipbarradas at sapo.pt
Wed Sep 26 23:53:08 CEST 2012


Hello,

Try the following.


sapply(seq_len(nrow(dat) - 4), function(i){
     w <- window(dat$value, start = i, end = i + 4)
     mean(w[w < 0])})

Hope this helps,

Rui Barradas
Em 26-09-2012 16:38, Eko andryanto Prakasa escreveu:
>   haiii
>
> i want to know, is there any script in R to measure looping averageif (like in the excel) .......
> for example:
> i have a vector
> row    value
> 1        0
> 2        2
> 3        -3
> 4        -2
> 5        1
> 6        -2
>
> i want to measure the average of the vector for negative value with window estimation 5
> so first mean is (-3+-2)/2
>       second mean is (-3+-2+-2)/3
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list