[R] Taking diff of character vectors

Sergey Goriatchev sergeyg at gmail.com
Fri Mar 13 11:24:22 CET 2009

Hello, everybody

Say I have
nm1 <- c(rep(1,10), rep(0,10))
then I can do:
to see where I have shift in value

but what if I have
nm2 <- c(rep("SPZ8", 10), rep("SPX9", 10))

how can I produce the same ouput as diff(nm1) does, that is zeros
everywhere except for one place where SPZ8 changes to SPX9 (there
should be 1 there)?

What if I have a matrix of characters like that:
nm3 <- c(rep("GLF9", 4), rep("GLF10", 16))
matr <- cbind(nm2, nm3)

How can I efficiently create two more columns that contain zeros
everywhere except for place where there is shift in character values?

Thanks for help!

I'm not young enough to know everything. /Oscar Wilde
Experience is one thing you can't get for nothing. /Oscar Wilde
When you are finished changing, you're finished. /Benjamin Franklin
Tell me and I forget, teach me and I remember, involve me and I learn.
/Benjamin Franklin
Luck is where preparation meets opportunity. /George Patten

More information about the R-help mailing list