[R] Successive subsets from a vector?

Gabor Grothendieck ggrothendieck at gmail.com
Tue Aug 22 20:29:07 CEST 2006


Here is a solution that uses gsub with a negative lookahead perl-style
regexp to do it:

VECTOR <- c(1,4,2,6,5,0,11,10,4,3,6,8,6)
e <- "([[:digit:]]+),(?=([[:digit:]]+),([[:digit:]]+),([[:digit:]]+),([[:digit:]]+))"
out <- gsub(e, "\\1\\2\\3\\4\\5 ", paste(VECTOR, collapse = ","), perl = TRUE)
head(strsplit(out, " ")[[1]], -1)  # uses head from R 2.4.0


On 8/22/06, kone <attenka at utu.fi> wrote:
> I'd like to pick every imbricated five character long subsets from a
> vector. I guess there is some efficient way to do this without loops...
> Here is a for-loop-version and a model for output:
>
> VECTOR=c(1,4,2,6,5,0,11,10,4,3,6,8,6);
>
> ADDRESSES=c();
> for(i in 1:(length(VECTOR)-4)){
>        ADDRESSES[i]=paste(VECTOR[i:(i+4)],collapse="")
> }
>
>  > ADDRESSES
> [1] "14265"   "42650"   "265011"  "6501110" "5011104" "0111043"
> "1110436" "104368"
> [9] "43686"
>
>
> Atte Tenkanen
> University of Turku, Finland
>
>        [[alternative text/enriched version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list