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?
Gabor Grothendieck
2009-Jan-25 22:42 UTC
[R] comparing the previous and next entry of a vector
Try this:> DF <- data.frame(x, nxt = c(tail(x, -1), NA), prv = c(NA, head(x, -1))) > DFx nxt prv 1 1 2 NA 2 2 3 1 3 3 4 2 4 4 5 3 5 5 2 4 6 2 6 5 7 6 NA 2> subset(DF, nxt == 3 & prv == 1)$x[1] 2 On Sun, Jan 25, 2009 at 5:29 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. >
is there a way to do that without generating a data.frame? In my real data, I have a big data.frame and I have to compare over different columns... Am 25.01.2009 um 23:42 schrieb Gabor Grothendieck:> Try this: > >> DF <- data.frame(x, nxt = c(tail(x, -1), NA), prv = c(NA, head(x, >> -1))) >> DF > x nxt prv > 1 1 2 NA > 2 2 3 1 > 3 3 4 2 > 4 4 5 3 > 5 5 2 4 > 6 2 6 5 > 7 6 NA 2 >> subset(DF, nxt == 3 & prv == 1)$x > [1] 2 > > > On Sun, Jan 25, 2009 at 5:29 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. >>
On 25-Jan-09 22:29:25, J?rg Gro? 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?x <- c(1,2,3,4,5,2,6) x0 <- x[1:(length(x)-2)] x1 <- x[3:(length(x))] x[which((x0==1)&(x1==3))+1] # [1] 2 Or, changing the data: x <- c(4,5,1,7,3,2,6,9,8) x0 <- x[1:(length(x)-2)] x1 <- x[3:(length(x))] x[which((x0==1)&(x1==3))+1] # [1] 7 Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 25-Jan-09 Time: 22:47:15 ------------------------------ XFMail ------------------------------
on 01/25/2009 04:29 PM J?rg Gro? 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?How about this: InBetween <- function(x, val1, val2) { unlist(sapply(2:(length(x) - 1), function(i) if ((x[i - 1] == val1) & (x[i + 1] == val2)) x[i])) }> InBetween(x, 1, 3)[1] 2> InBetween(x, 4, 2)[1] 5 It will return NULL if not found. You might want to reinforce it with some error checking as well. HTH, Marc Schwartz
Gabor Grothendieck
2009-Jan-25 23:11 UTC
[R] comparing the previous and next entry of a vector
The data frame is not essential. I was just trying to keep things tidy. Try this: nxt <- c(tail(x, -1), NA) prv <- c(NA, head(x, -1)) x[nxt == 3 & prv == 1] On Sun, Jan 25, 2009 at 5:53 PM, J?rg Gro? <joerg at licht-malerei.de> wrote:> is there a way to do that without generating a data.frame? > > In my real data, I have a big data.frame and I have to compare over > different columns... > > > Am 25.01.2009 um 23:42 schrieb Gabor Grothendieck: > >> Try this: >> >>> DF <- data.frame(x, nxt = c(tail(x, -1), NA), prv = c(NA, head(x, -1))) >>> DF >> >> x nxt prv >> 1 1 2 NA >> 2 2 3 1 >> 3 3 4 2 >> 4 4 5 3 >> 5 5 2 4 >> 6 2 6 5 >> 7 6 NA 2 >>> >>> subset(DF, nxt == 3 & prv == 1)$x >> >> [1] 2 >> >> >> On Sun, Jan 25, 2009 at 5:29 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. >>> > > ______________________________________________ > 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. >