Skip to content
Prev 305794 / 398506 Next

extracting column at regular intervals starting from different columns

On 18-09-2012, at 19:21, eliza botto wrote:

            
I have defined 2 more functions for David's and Clint's solution and my solution.

f3 <- function(x,M,start=1) {
    x[c(rep(FALSE,start-1),TRUE,rep(FALSE,M-start))]
} 

f4 <- function(x,M,start=1) {
    x[c(start,seq_len((length(x)-1)/M)*M+start)]
}

z3 <- f3(a,M,start=1)
z4 <- f4(a,M,start=1) 
identical(z3,z1)
identical(z3,z2)

z5 <- f3(a,M,start=4)
z6 <- f4(a,M,start=4)
identical(z5,z6)

With

N <- 444
a <- 1:N
M <- 37 

I get the following timing results.
test replications elapsed relative user.self sys.self
 1 f3(a, M, start = 4)       100000   1.621    1.562     1.606    0.014
 2 f4(a, M, start = 4)       100000   1.038    1.000     1.035    0.002

Compared to the timings for start=1 (previous post) the solution with rep becomes slightly slower.

Berend