Skip to content

extract vector elements of unknown range

7 messages · Sepp Tannhuber, William Dunlap, michael.weylandt at gmail.com (R. Michael Weylandt +1 more

#
Dear all,

I have two vectors
? x <- c(1:20)
? y <- c(1,5,10,14)

Now I would like to extract
? x[ (y[n] + 2):(y[n+1] - 1) ]
for all elements except last one in y. This means I want to have
??x[ c( (y[1]+2):(y[2]-1), (y[2]+2):(y[3]-1), (y[3]+2):(y[4]-1) ) ]
How is this possible if y is a vector of unknown length?

Best regards
Joseph
#
Hi,

try below:

x <- c(1:20)
y <- c(1, 5, 10, 14)

x[ c( (y[1]+2):(y[2]-1), (y[2]+2):(y[3]-1), (y[3]+2):(y[4]-1) ) ]
x[unlist(lapply(1:(length(y) - 1), function (i) (y[i] + 2) : (y[i + 1] - 1)))]
x[unlist(mapply(seq, y[-length(y)] + 2, y[-1] - 1, SIMPLIFY = FALSE))]
#
Hi,

thanks for your quick answers! These solve my problem.

Now I have another question. I think I can use?
? head(y, -1)
instead of
? y[-length(y)]

Are there differences in terms of performance?

Best regards
Joseph
#
Using system.time() you can check out the performance yourself.  E.g.,
the following compares head(y,-1), y[-length(y)], and y[seq_len(length(y)-1))].

   > y <- 1:10
   > all.equal(head(y,-1), y[-length(y)])
   [1] TRUE
   > all.equal(head(y,-1), y[seq_len(length(y)-1)])
   [1] TRUE
   > system.time(for(i in 1:1e5)head(y,-1))
      user  system elapsed 
      1.53    0.00    1.53 
   > system.time(for(i in 1:1e5)y[-length(y)])
      user  system elapsed 
      0.09    0.00    0.10 
   > system.time(for(i in 1:1e5)y[seq_len(length(y)-1)])
      user  system elapsed 
      0.11    0.00    0.11

Performance may depend on the size or type of the input, so check it on
data similar to what you intend to work with.  E.g., for long vectors the
seq_len approach looks better.
   > y <- runif(1e6)
   > system.time(for(i in 1:100)head(y,-1))
      user  system elapsed 
      1.26    0.25    1.51 
   > system.time(for(i in 1:100)y[-length(y)])
      user  system elapsed 
      1.10    0.36    1.45 
   > system.time(for(i in 1:100)y[seq_len(length(y)-1)])
      user  system elapsed 
      0.67    0.28    0.95

Also note that the relative performance of these idioms may change as R evolves.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
On Aug 25, 2012, at 4:23 AM, Sepp Tannhuber <sepp.tannhuber at yahoo.de> wrote:

            
The latter is marginally more efficient. Take a look at head.matrix()

Cheers,
Michael
#
Hi,

Yes you can. As William says, the 'seq_len' approach seems to be better.
'head' function is the wrapper for 'seq_len' approach and slower.

I didn't know that '-length(x)' approach is slow for long vectors....