R: all.equal() for multiple objects? -


what best way compare more 2 objects all.equal()?

here's 1 way:

foo <- c(1:10) bar <- letters[1:10] baz <- c(1:10)  # doesn't work because all.equal() returns character vector when objects not equal   all(sapply(list(bar, baz), all.equal, foo))  # works   mode(sapply(list(bar, baz), all.equal, foo)) == "logical" #false    bar <- c(1:10)    mode(sapply(list(bar, baz), all.equal, foo)) == "logical" #true 

update: @brodieg pointed out one-liner above tells whether objects equal or not, whereas all.equal() tells isn't equal them if aren't equal.

here option:

objs <- mget(c("foo", "bar", "faz")) outer(objs, objs, vectorize(all.equal)) 

it's better yours because detect when bar , faz same, when foo isn't. said, lot of unnecessary comparisons , slow. example, if change foo letters[1:10] get:

    foo         bar         faz         foo true        character,2 character,2 bar character,2 true        true        faz character,2 true        true  

for details on went wrong, subset:

outer(objs, objs, vectorize(all.equal))[1, 2] 

produces:

[[1]] [1] "modes: character, numeric"               [2] "target character, current numeric"        

if care objects must all.equal, solution pretty good.

also, per comments limit of duplicate calculations:

res <- outer(objs, objs, function(x, y) vector("list", length(x))) combs <- combn(seq(objs), 2) res[t(combs)] <- vectorize(all.equal)(objs[combs[1,]], objs[combs[2,]]) res 

produces

    foo  bar         faz         foo null character,2 character,2 bar null null        true        faz null null        null          

this still shows full matrix, makes obvious comparisons produced what.


Comments