Jörg Groß
2009-Jan-25 22:02 UTC
[R] comparing the previous and next entry of a vector to a criterium
Hi, I have a quit abstract problem, hope someone can help me here. I have a vector like this: x <- c(1,2,3,4,5,2,6) x [1] 1 2 3 4 5 2 6 now I want to get the number where the previous number is 1 and the next number is 3 (that is the 2 at the second place) I tried something with tail(x, -1) ... with that, I can check the next number, but how can I check the previous number?
Kingsford Jones
2009-Jan-25 23:21 UTC
[R] comparing the previous and next entry of a vector to a criterium
How about: a <- c(1,2,3,3,2,1,6,3,2) b <- c(NA,a[-length(a)]) c <- c(a[-1],NA) a[b==1 & c==3] [1] 2 6 hth, Kingsford Jones On Sun, Jan 25, 2009 at 3:02 PM, J?rg Gro? <joerg at licht-malerei.de> wrote:> Hi, > > I have a quit abstract problem, hope someone can help me here. > > I have a vector like this: > > > x <- c(1,2,3,4,5,2,6) > x > > [1] 1 2 3 4 5 2 6 > > now I want to get the number where the previous number is 1 and the next > number is 3 > (that is the 2 at the second place) > > I tried something with tail(x, -1) ... > with that, I can check the next number, but how can I check the previous > number? > > ______________________________________________ > 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. >