Skip to content

extracting column and regular interval in R

10 messages · R. Michael Weylandt, Eliza Botto, David Winsemius +2 more

#
x[c(TRUE, rep(FALSE, 36)),]

and let recycling work its magic!

To concretize:

x <- 1:100

x[c(TRUE, rep(FALSE, 4))]

Cheers,
Michael
On Tue, Sep 18, 2012 at 3:55 PM, eliza botto <eliza_botto at hotmail.com> wrote:
#
On 18-09-2012, at 16:55, eliza botto wrote:

            
Indexing with 

c(1,seq_len((length(x)-1)/M)*M+1)

is also an option.
It appears to be faster than Michael's method.

N <- 444
a <- 1:N

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

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

M <- 37 
M
z1 <- f1(a,M)
z2 <- f2(a,M)
[1] TRUE


Nrep <- 100000

library(rbenchmark)
benchmark(f1(a,M), f2(a,M), replications=Nrep)
test replications elapsed relative user.self sys.self user.child
1 f1(a, M)       100000   1.395    1.437     1.367    0.027          0
2 f2(a, M)       100000   0.971    1.000     0.959    0.011          0

For N <- 4440 the timings are
test replications elapsed relative user.self sys.self user.child
1 f1(a, M)       100000   4.636    2.483     4.569    0.065          0
2 f2(a, M)       100000   1.867    1.000     1.714    0.153          0


Berend
#
On Sep 18, 2012, at 10:21 AM, eliza botto wrote:

            
This will start at 4 and then be recycled every succeeding 37th item:

offset1= 3; offset2 = 37
c( rep(FALSE, 3), TRUE, rep(FALSE, offset2-offset1-1) )

Observe:
[1]  4 41 78
David Winsemius, MD
Alameda, CA, USA
#
On 18-09-2012, at 19:21, eliza botto wrote:

            
When you use my method you could do the following

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

M <- 37
A <- 1:(3*M)
f3(A,M)
f3(A,M,start=1)
f3(A,M,start=2)

When start is too large you may get NA's in the result.
You may have to get rid of them.

Berend
#
Try:

x <- 1:444
start<-2
# [1]   2  39  76 113 150 187 224 261 298 335 372 409
start<-30
x[c(rep(FALSE,start-1),TRUE, rep(FALSE, 36-start+1))]
# [1]  30  67 104 141 178 215 252 289 326 363 400 437
start<-37
x[c(rep(FALSE,start-1),TRUE, rep(FALSE, 36-start+1))]
# [1]  37  74 111 148 185 222 259 296 333 370 407 444



Clint Bowman			INTERNET:	clint at ecy.wa.gov
Air Quality Modeler		INTERNET:	clint at math.utah.edu
Department of Ecology		VOICE:		(360) 407-6815
PO Box 47600			FAX:		(360) 407-7534
Olympia, WA 98504-7600

         USPS:           PO Box 47600, Olympia, WA 98504-7600
         Parcels:        300 Desmond Drive, Lacey, WA 98503-1274
On Tue, 18 Sep 2012, eliza botto wrote:

            
#
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