[R] how to checking whether elements of a vector changed or not.

liujb liujulia7 at yahoo.com
Mon Jun 1 17:57:13 CEST 2009


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.




More information about the R-help mailing list