Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

R %in% operator for RefClass objects

$
0
0

I've the following problem. I know how to do a workaround for this but its more of a esthetic thing:

A <- setRefClass("A", fields = list(a = "character"))

A$methods(
  equal = function(obj){
    a == obj$a
  }
)

setMethod("==",signature = c("A","A"), function(e1,e2){e1$equal(e2)})

a1 <- A(a="xxx")
a2 <- A(a="yyy")
a3 <- A(a="xxx")

a1 == a2 # FALSE
a1 == a3 # TRUE

A1 <- sapply(c("a","b","c","d"),function(x)A(a=x))
A2 <- sapply(c("c","d","e"),function(x)A(a=x))

A1 
A2   

A2 %in% A1 # TRUE TRUE TRUE TRUE but I would expect FALSE FALSE TRUE TRUE

A1 %in% A2 # TRUE TRUE TRUE.     but I would expect TRUE TRUE FALSE  

As you can see, the equality operator works fine if set up for a RefClass. But the %in% operator doesn't. It just returns TRUE for all elements. Why is that? I know the operator is based on the match function, but what type of comparison is used insight? Any ideas?


Viewing all articles
Browse latest Browse all 201839

Trending Articles