Skip to content

compare strings

2 messages · Peter Dalgaard

#
Bernd Jagla wrote:
If they really are factors with different level sets, I think you might 
prefer as.character() there.

 > x <- factor(c("a","b"))
 > y <- factor(c("b","c"))
 > x==y
Error in Ops.factor(x, y) : level sets of factors are different
 > as.integer(x)==as.integer(y)
[1] TRUE TRUE
 > as.character(x)==as.character(y)
[1] FALSE FALSE

Also, extending the above slightly:
 > d <- as.character(x)==as.character(y)
 > table(d)
d
FALSE  TRUE
    2     1
 > which(!d)
[1] 1 2

  
    
#
Peter Dalgaard wrote:
Oups. Two lines fell out. I meant:

 > y <- factor(c("b","c","b"))
 > x <- factor(c("a","b","b"))
 > d <- as.character(x)==as.character(y)
 > table(d)
d
FALSE  TRUE
    2     1
 > which(!d)
[1] 1 2