[R] Simple - Finding vector in a vector
Steve Lianoglou
mailinglist.honeypot at gmail.com
Mon Oct 8 16:04:24 CEST 2012
Hi Mike,
On Mon, Oct 8, 2012 at 9:38 AM, Mike Spam <ichmagspam at googlemail.com> wrote:
> Sorry, i just realized, that it output the sum of all vectors. I can
> work with this function but it would be much faster and easier if it
> would be possible to get the positions of evry match.
>
> example:
>
> NA 1 NA 1 1 1 1 1 1 NA 1
>
> rle returns
> lengths: int [1:6] 1 1 1 6 1 1
>
> what i need would be something like,
> 1 1 1 3 3 3 3 1 1
Somehow peculiar ;-)
This gets you somehow close -- but I think this must be what you mean,
so ... let's see:
R> x <- c(NA, 1, NA, 1, 1, 1, 1, 1, 1, NA, 1)
R> e <- embed(x, e) ## Take a look at this matrix
R> r <- apply(e, 1, rle)
R> sapply(r, function(rr) rr$lengths[1])
## [1] 1 1 2 3 3 3 3 1 1
If your input vector (`x` here) is large, the call to `embed` may be painful.
HTH,
-steve
--
Steve Lianoglou
Graduate Student: Computational Systems Biology
| Memorial Sloan-Kettering Cancer Center
| Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
More information about the R-help
mailing list