Skip to content

Simple - Finding vector in a vector

10 messages · Rui Barradas, Steve Lianoglou, Jessica Streicher +3 more

#
Hi,

just a simple question.
Assumed i have a vector,

FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE
or
NA  1  1  1 NA  1 NA  1 NA

what i need is the position where an element is the same - three (or
in general multiple) times in a row.

in this case: i want to get the position where it is TRUE TRUE TRUE or 1 1 1
it doesn't matter if it is the first, middle or last element. So the
output could be 2 or 3 or 4

My idea would be to lag the vector and calc differences... but i would
prefer any build in (or time saving) function. :)

thanks,

Nico
#
Hello,

See ?rle

Hope this helps,

Rui Barradas
Em 08-10-2012 13:55, Mike Spam escreveu:
#
Hey Rui,

Perfect! Thanks!! :)

Nico

2012/10/8 Rui Barradas <ruipbarradas at sapo.pt>:
#
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

but anyway i can work with rle, if there is no suitable function.

thanks,
Nico



2012/10/8 Mike Spam <ichmagspam at googlemail.com>:
#
Hi Mike,
On Mon, Oct 8, 2012 at 9:38 AM, Mike Spam <ichmagspam at googlemail.com> wrote:
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
#
Ugh, typo:

On Mon, Oct 8, 2012 at 10:04 AM, Steve Lianoglou
<mailinglist.honeypot at gmail.com> wrote:

            
The 2nd param to embed should be 3, so:

R> x <- c(NA,  1, NA,  1,  1,  1,  1,  1,  1, NA,  1)
R> e <- embed(x, 3) ## 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

Sorry for the confusion ... e and 3 are so close ;-)

-st3v3
#
[,1] [,2] [,3]
 [1,]   NA    1   NA
 [2,]    1   NA    1
 [3,]    1    1   NA
 [4,]    1    1    1
 [5,]    1    1    1
 [6,]    1    1    1
 [7,]    1    1    1
 [8,]   NA    1    1
 [9,]    1   NA    1
[1] 4 5 6 7

gives you the first of the 3
On 08.10.2012, at 15:38, Mike Spam wrote:

            
#
Building on Jessica and Steve's use of embed:
[1] 1 1 1 3 3 3 3 1 1

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
#
Great, works perfekt now!
Thanks for your fast help!

Nico


2012/10/8 David L Carlson <dcarlson at tamu.edu>:
#
On 08-10-2012, at 18:00, Mike Spam wrote:

            
See this discussion for a variety of solutions: http://r.789695.n4.nabble.com/matching-a-sequence-in-a-vector-td4389523.html#a4393453
(or here https://stat.ethz.ch/pipermail/r-help/2012-February/303608.html )
There are some very fast ones.

If the vector in which you are searching contains NA's you will have to replace them by 0 or something.

Berend