Re: [R] extract vector elements of unknown range [SOLVED]

2012-08-26 Thread Sepp Tannhuber
Thank you all for your answers! Enjoy the rest of the weekend Joseph __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, mi

Re: [R] extract vector elements of unknown range

2012-08-25 Thread Noia Raindrops
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 -- Noia Raindrops noia.raindr...@gmail.com __

Re: [R] extract vector elements of unknown range

2012-08-25 Thread R. Michael Weylandt
On Aug 25, 2012, at 4:23 AM, Sepp Tannhuber wrote: > 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? The latter is marginally

Re: [R] extract vector elements of unknown range

2012-08-25 Thread William Dunlap
these idioms may change as R evolves. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Sepp Tannhuber > Sent: Saturday, August 25, 2012 2:24 AM > To:

Re: [R] extract vector elements of unknown range

2012-08-25 Thread Sepp Tannhuber
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 __ R-help@r-project.org mailing lis

Re: [R] extract vector elements of unknown range

2012-08-25 Thread Noia Raindrops
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))] -- Noia Raindrops noia.raindr