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
r-help@r-project.org > Subject: Re: [R] extract vector elements of unknown range > > 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[-lengt

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

[R] extract vector elements of unknown range

2012-08-24 Thread Sepp Tannhuber
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