liujb
2009-Jun-01 15:57 UTC
[R] how to checking whether elements of a vector changed or not.
Hello, I have a vector: x <- c("A", "A", "A", "B", "A", "A", "C") I'd like to compare each of elements of vector x from its previous element (except for the 1st element which does not have previous element). So I'd like to get a vector y (of same length) that looks something like (0, 0, 0, 1, 1, 0, 1) or (F, F, F, T, T, F, T) Is there a command that do this? I was thinking to construct another variable x1 from x as follows: x1 <- vector(length=length(x)) x1[1] <- x[1] x1[2:length(x)] <- x[1:(length(x)-1)] x1 Now, x1 is ("A" "A" "B" "B" "B" "A"). However, I don't know how to compare 2 vectors (of same length) elements by elements and a vector that indicates change or no change. Or there is a easier way to do this? Thank you very much Julia -- View this message in context: http://www.nabble.com/how-to-checking-whether-elements-of-a-vector-changed-or-not.-tp23817310p23817310.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2009-Jun-01 22:21 UTC
[R] how to checking whether elements of a vector changed or not.
Try this: c(FALSE, !!diff(c(factor(x)))) On Mon, Jun 1, 2009 at 11:57 AM, liujb <liujulia7 at yahoo.com> wrote:> > Hello, > > I have a vector: > x <- c("A", "A", "A", "B", "A", "A", "C") > > I'd like to compare each of elements of vector x from its previous element > (except for the 1st element which does not have previous element). So I'd > like to get a vector y (of same length) that looks something like > (0, 0, 0, 1, 1, 0, 1) or > (F, F, F, T, T, F, T) > > Is there a command that do this? > > I was thinking to construct another variable x1 from x as follows: > x1 <- vector(length=length(x)) > x1[1] <- x[1] > x1[2:length(x)] <- x[1:(length(x)-1)] > x1 > > Now, x1 is ("A" "A" "B" "B" "B" "A"). However, I don't know how to compare 2 > vectors (of same length) elements by elements and a vector that indicates > change or no change. > > Or there is a easier way to do this? > > Thank you very much > Julia > > > > -- > View this message in context: http://www.nabble.com/how-to-checking-whether-elements-of-a-vector-changed-or-not.-tp23817310p23817310.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Linlin Yan
2009-Jun-02 19:35 UTC
[R] how to checking whether elements of a vector changed or not.
> c(x[1], x[-length(x)]) != x[1] FALSE FALSE FALSE TRUE TRUE FALSE TRUE On Mon, Jun 1, 2009 at 11:57 PM, liujb <liujulia7 at yahoo.com> wrote:> > Hello, > > I have a vector: > x <- c("A", "A", "A", "B", "A", "A", "C") > > I'd like to compare each of elements of vector x from its previous element > (except for the 1st element which does not have previous element). So I'd > like to get a vector y (of same length) that looks something like > (0, 0, 0, 1, 1, 0, 1) or > (F, F, F, T, T, F, T) > > Is there a command that do this? > > I was thinking to construct another variable x1 from x as follows: > x1 <- vector(length=length(x)) > x1[1] <- x[1] > x1[2:length(x)] <- x[1:(length(x)-1)] > x1 > > Now, x1 is ("A" "A" "B" "B" "B" "A"). However, I don't know how to compare 2 > vectors (of same length) elements by elements and a vector that indicates > change or no change. > > Or there is a easier way to do this? > > Thank you very much > Julia > > > > -- > View this message in context: http://www.nabble.com/how-to-checking-whether-elements-of-a-vector-changed-or-not.-tp23817310p23817310.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >