Skip to content
Prev 173591 / 398506 Next

Taking diff of character vectors

2009/3/13 Sergey Goriatchev <sergeyg at gmail.com>:
You could convert to "factor" and then to numeric:

 > nm2 <- c(rep("SPZ8", 10), rep("SPX9", 10))

 > diff(as.numeric(as.factor(nm2)))
 [1]  0  0  0  0  0  0  0  0  0 -1  0  0  0  0  0  0  0  0  0

 It might be that your character values should really be factors
anyway - check out some R docs on what factors are and what they can
do for you. That also means you might want your matrix to be a data
frame, since a matrix can't contain your character values and numeric
0/1 values. Data frames can! If you try it with a matrix you end up
getting character zeroes and minus ones:

 > matr <- cbind(nm2, nm3)

 > matr = cbind(matr,c(0,diff(as.numeric(as.factor(matr[,1])))))

 > matr[8:12,]
     nm2    nm3
[1,] "SPZ8" "GLF10" "0"
[2,] "SPZ8" "GLF10" "0"
[3,] "SPZ8" "GLF10" "0"
[4,] "SPX9" "GLF10" "-1"
[5,] "SPX9" "GLF10" "0"

Easier with factors in data frames:

 > df=data.frame(nm2=as.factor(nm2),nm3=as.factor(nm3))

 > df$dnm2 = c(0,diff(as.numeric(df$nm2)))
 > df[8:12,]
    nm2   nm3 dnm2
8  SPZ8 GLF10    0
9  SPZ8 GLF10    0
10 SPZ8 GLF10    0
11 SPX9 GLF10   -1
12 SPX9 GLF10    0

Barry