[R] extract vector elements of unknown range

Noia Raindrops noia.raindrops at gmail.com
Sat Aug 25 09:24:35 CEST 2012


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.raindrops at gmail.com




More information about the R-help mailing list