Skip to content
Prev 205979 / 398503 Next

apply a function down each column

On Jan 11, 2010, at 9:18 AM, Steve Lianoglou wrote:

            
library(zoo)
?rollapply

 > dat <- letters[sample(1:20)]
 > dat
  [1] "l" "d" "o" "s" "j" "k" "t" "r" "g" "b" "e" "q" "f" "i" "c" "n"  
"m" "p" "a" "h"
# using "<" as a pairwise comparison as an example
 > zdat <- zoo(dat, as.Date(1:20))
 > rollapply(zdat, width=2, FUN=function(x) {x[1] < x[2]} , by=2)
1970-01-02 1970-01-04 1970-01-06 1970-01-08 1970-01-10 1970-01-12  
1970-01-14 1970-01-16 1970-01-18 1970-01-20
      FALSE       TRUE       TRUE      FALSE      FALSE        
TRUE       TRUE       TRUE       TRUE       TRUE

The date labels are arbitrary. If youy want them to be removed you can  
wrap as.vector() around the rollapply call.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT