[R] bad performance of a function

Petr Pikal petr.pikal at precheza.cz
Fri Nov 14 13:35:52 CET 2003


Dear all

I need to find a length of true sequences in logical vector (see example 1). I found 
a possible solution which is good but if I use it on a larger data set I experience a 
substantial decrease in performance (example 2).

Example 1
set.seed(111)
x <- sample(c(T,F),50, replace=T)
system.time(cetnost <- as.numeric(table(which(x)-cumsum(x[which(x)]))))
[1] 0.00 0.00 0.03   NA   NA
cetnost
[1] 1 3 2 5 1 4 1 1 1 3 1 1 2

Example 2
x<-sample(c(T,F),40321*51, replace=T)
dd<-matrix(x,40321,51)
system.time(cetnost <- lapply(dd,function(x) as.numeric(table(which(x)-
cumsum(x[which(x)])))))
Timing stopped at: 750.63 1 775.6 NA NA 

Please give me any hint how to improve performance or advice a different (but 
more effective) solution.

R 1.8.0, W2000,  512M memory, Pentium4

Thank you in advance.



Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list