Behavior of identical() in apply in R

。_饼干妹妹 提交于 2019-12-02 02:27:58

问题


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.



来源:https://stackoverflow.com/questions/47213268/behavior-of-identical-in-apply-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!