Behavior of identical() in apply in R

2019-02-27 02:12发布

问题:

This is weird.

apply( matrix(c(1,NA,2,3,NA,NA,2,4),ncol = 2), 1, function(x) identical(x[1], x[2]) )
#[1] FALSE  TRUE  TRUE FALSE
apply( data.frame(a = c(1,NA,2,3),b = c(NA,NA,2,4)), 1, function(x) identical(x[1], x[2]) )
#[1] FALSE FALSE FALSE FALSE
apply( as.matrix(data.frame(a = c(1,NA,2,3),b = c(NA,NA,2,4))), 1, function(x) identical(x[1], x[2]) )
#[1] FALSE FALSE FALSE FALSE

This is due to the names attribute as indicated below by joran. I can obtain the result I expected by:

apply( data.frame(a = c(1,NA,2,3),b = c(NA,NA,2,4)), 1, function(x) identical(unname(x[1]), unname(x[2])) ) 

or:

apply( data.frame(a = c(1,NA,2,3),b = c(NA,NA,2,4)), 1, function(x) identical(x[[1]], x[[2]]) ) 

Is there a more natural way to approach this? It would seem that there should be an option to ignore attributes, like in all.equal().

回答1:

Probably

mapply(identical, x$a, x$b)
#[1] FALSE  TRUE  TRUE FALSE 

where x is a data frame.

As an aside, using apply with a data frame is almost always a mistake. It will coerce the data frame to a matrix which often leads to unexpected results.



标签: r compare apply