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
Simple - Finding vector in a vector
10 messages · Rui Barradas, Steve Lianoglou, Jessica Streicher +3 more
Hello, See ?rle Hope this helps, Rui Barradas Em 08-10-2012 13:55, Mike Spam escreveu:
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
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
Hey Rui, Perfect! Thanks!! :) Nico 2012/10/8 Rui Barradas <ruipbarradas at sapo.pt>:
Hello, See ?rle Hope this helps, Rui Barradas Em 08-10-2012 13:55, Mike Spam escreveu:
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
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
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>:
Hey Rui, Perfect! Thanks!! :) Nico 2012/10/8 Rui Barradas <ruipbarradas at sapo.pt>:
Hello, See ?rle Hope this helps, Rui Barradas Em 08-10-2012 13:55, Mike Spam escreveu:
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
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
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
Ugh, typo: On Mon, Oct 8, 2012 at 10:04 AM, Steve Lianoglou
<mailinglist.honeypot at gmail.com> wrote:
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
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
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
x<-c(NA , 1 ,NA, 1 , 1 , 1 , 1 , 1 ,1 ,NA , 1) embed(x,3)
[,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
which(rowSums(embed(x,3))==3)
[1] 4 5 6 7 gives you the first of the 3
On 08.10.2012, at 15:38, Mike Spam 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 but anyway i can work with rle, if there is no suitable function. thanks, Nico 2012/10/8 Mike Spam <ichmagspam at googlemail.com>:
Hey Rui, Perfect! Thanks!! :) Nico 2012/10/8 Rui Barradas <ruipbarradas at sapo.pt>:
Hello, See ?rle Hope this helps, Rui Barradas Em 08-10-2012 13:55, Mike Spam escreveu:
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
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
Building on Jessica and Steve's use of embed:
X <- c(NA, 1, NA, 1, 1, 1, 1, 1, 1, NA, 1) Z <- rowSums(embed(X, 3)) Z[is.na(Z)] <- 1 Z
[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
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Jessica Streicher Sent: Monday, October 08, 2012 9:19 AM To: Mike Spam Cc: r-help at r-project.org Subject: Re: [R] Simple - Finding vector in a vector
x<-c(NA , 1 ,NA, 1 , 1 , 1 , 1 , 1 ,1 ,NA , 1) embed(x,3)
[,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
which(rowSums(embed(x,3))==3)
[1] 4 5 6 7 gives you the first of the 3 On 08.10.2012, at 15:38, Mike Spam 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 but anyway i can work with rle, if there is no suitable function. thanks, Nico 2012/10/8 Mike Spam <ichmagspam at googlemail.com>:
Hey Rui, Perfect! Thanks!! :) Nico 2012/10/8 Rui Barradas <ruipbarradas at sapo.pt>:
Hello, See ?rle Hope this helps, Rui Barradas Em 08-10-2012 13:55, Mike Spam escreveu:
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
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
Great, works perfekt now! Thanks for your fast help! Nico 2012/10/8 David L Carlson <dcarlson at tamu.edu>:
Building on Jessica and Steve's use of embed:
X <- c(NA, 1, NA, 1, 1, 1, 1, 1, 1, NA, 1) Z <- rowSums(embed(X, 3)) Z[is.na(Z)] <- 1 Z
[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
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Jessica Streicher Sent: Monday, October 08, 2012 9:19 AM To: Mike Spam Cc: r-help at r-project.org Subject: Re: [R] Simple - Finding vector in a vector
x<-c(NA , 1 ,NA, 1 , 1 , 1 , 1 , 1 ,1 ,NA , 1) embed(x,3)
[,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
which(rowSums(embed(x,3))==3)
[1] 4 5 6 7 gives you the first of the 3 On 08.10.2012, at 15:38, Mike Spam 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 but anyway i can work with rle, if there is no suitable function. thanks, Nico 2012/10/8 Mike Spam <ichmagspam at googlemail.com>:
Hey Rui, Perfect! Thanks!! :) Nico 2012/10/8 Rui Barradas <ruipbarradas at sapo.pt>:
Hello, See ?rle Hope this helps, Rui Barradas Em 08-10-2012 13:55, Mike Spam escreveu:
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
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
______________________________________________ R-help at 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, minimal, self-contained, reproducible code.
On 08-10-2012, at 18:00, Mike Spam wrote:
Great, works perfekt now! Thanks for your fast help!
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