我完全没有找到关于这个特定问题的其他r-help或Stack Overflow讨论.对不起,如果它显而易见的话.我相信我只是在寻找让R的==符号永远不会返回NAs的最简单方法.
# Example #
# Say I have two vectors
a <- c( 1 , 2 , 3 )
b <- c( 1 , 2 , 4 )
# And want to test if each element in the first
# is identical to each element in the second:
a == b
# It does what I want perfectly:
# TRUE TRUE FALSE
# But if either vector contains a missing,
# the `==` operator returns an incorrect result:
a <- c( 1 , NA , 3 )
b <- c( 1 , NA , 4 )
# Here I'd want TRUE TRUE FALSE
a == b
# But I get TRUE NA FALSE
a <- c( 1 , NA , 3 )
b <- c( 1 , 2 , 4 )
# Here I'd want TRUE FALSE FALSE
a == b
# But I get TRUE NA FALSE again.
Run Code Online (Sandbox Code Playgroud)
我得到了我想要的结果:
mapply( `%in%` , a , b )
Run Code Online (Sandbox Code Playgroud)
但mapply对我来说似乎很苛刻.
有更直观的解决方案吗?
Cat*_*ath 25
另一个选择,但它比mapply('%in%', a , b)?更好?:
(!is.na(a) & !is.na(b) & a==b) | (is.na(a) & is.na(b))
Run Code Online (Sandbox Code Playgroud)
按照@AnthonyDamico的建议,创建了"mutt"运算符:
"%==%" <- function(a, b) (!is.na(a) & !is.na(b) & a==b) | (is.na(a) & is.na(b))
Run Code Online (Sandbox Code Playgroud)
编辑:或者,由@Frank稍微不同和更短的版本(这也更有效)
"%==%" <- function(a, b) (is.na(a) & is.na(b)) | (!is.na(eq <- a==b) & eq)
Run Code Online (Sandbox Code Playgroud)
有了不同的例子:
a <- c( 1 , 2 , 3 )
b <- c( 1 , 2 , 4 )
a %==% b
# [1] TRUE TRUE FALSE
a <- c( 1 , NA , 3 )
b <- c( 1 , NA , 4 )
a %==% b
# [1] TRUE TRUE FALSE
a <- c( 1 , NA , 3 )
b <- c( 1 , 2 , 4 )
a %==% b
#[1] TRUE FALSE FALSE
a <- c( 1 , NA , 3 )
b <- c( 3 , NA , 1 )
a %==% b
#[1] FALSE TRUE FALSE
Run Code Online (Sandbox Code Playgroud)
akr*_*run 13
你可以试试
replace(a, is.na(a), Inf)==replace(b, is.na(b), Inf)
Run Code Online (Sandbox Code Playgroud)
或@docendo discimus提出的更快的变化
replace(a, which(is.na(a)), Inf)==replace(b, which(is.na(b)), Inf)
Run Code Online (Sandbox Code Playgroud)
基于不同的场景
1.
a <- c( 1 , 2 , 3 )
b <- c( 1 , 2 , 4 )
akrun1()
#[1] TRUE TRUE FALSE
Run Code Online (Sandbox Code Playgroud)
2.
a <- c( 1 , NA , 3 )
b <- c( 1 , NA , 4 )
akrun1()
#[1] TRUE TRUE FALSE
Run Code Online (Sandbox Code Playgroud)
3.
a <- c( 1 , NA , 3 )
b <- c( 1 , 2 , 4 )
akrun1()
#[1] TRUE FALSE FALSE
Run Code Online (Sandbox Code Playgroud)
set.seed(24)
a <- sample(c(1:10, NA), 1e6, replace=TRUE)
b <- sample(c(1:20, NA), 1e6, replace=TRUE)
akrun1 <- function() {replace(a, is.na(a), Inf)==replace(b, is.na(b), Inf)}
cathG <- function() {(!is.na(a) & !is.na(b) & a==b) | (is.na(a) & is.na(b))}
anthony <- function() {mapply(`%in%`, a, b)}
webb <- function() {ifelse(is.na(a),is.na(b),a==b)}
docend <- function() {replace(a, which(is.na(a)), Inf)==replace(b,
which(is.na(b)), Inf)}
library(microbenchmark)
microbenchmark(akrun1(), cathG(), anthony(), webb(),docend(),
unit='relative', times=20L)
#Unit: relative
# expr min lq mean median uq max
# akrun1() 3.050200 3.035625 3.007196 2.963916 2.977490 3.083658
# cathG() 4.829972 4.893266 4.843585 4.790466 4.816472 4.939316
# anthony() 190.499027 224.389971 215.792965 217.647702 215.503308 212.356051
# webb() 14.000363 14.366572 15.412527 14.095947 14.671741 19.735746
# docend() 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
# neval cld
# 20 a
# 20 a
# 20 c
# 20 b
# 20 a
Run Code Online (Sandbox Code Playgroud)
假设我们没有很大的相对数NA,建议的矢量化解决方案浪费了一些资源来比较已经解决的值a==b.
我们通常可以假设它NAs很少,所以它首先值得计算a==b然后NAs单独处理,尽管有额外的步骤和临时变量:
`%==%` <- function(a,b){
x <- a==b
na_x <- which(is.na(x))
x[na_x] <- is.na(a[na_x]) & is.na(b[na_x])
x
}
Run Code Online (Sandbox Code Playgroud)
检查输出
a <- c( 1 , 2 , 3 )
b <- c( 1 , 2 , 4 )
a %==% b
# [1] TRUE TRUE FALSE
a <- c( 1 , NA , 3 )
b <- c( 1 , NA , 4 )
a %==% b
# [1] TRUE TRUE FALSE
a <- c( 1 , NA , 3 )
b <- c( 1 , 2 , 4 )
a %==% b
# [1] TRUE FALSE FALSE
Run Code Online (Sandbox Code Playgroud)
基准
我只使用最快的解决方案再现@ akrun的基准,n = 100.
set.seed(24)
a <- sample(c(1:10, NA), 1e6, replace=TRUE)
b <- sample(c(1:20, NA), 1e6, replace=TRUE)
mm <- function(){
x <- a==b
na_x <- which(is.na(x))
x[na_x] <- is.na(a[na_x]) & is.na(b[na_x])
x
}
akrun1 <- function() {replace(a, is.na(a), Inf)==replace(b, is.na(b), Inf)}
cathG <- function() {(!is.na(a) & !is.na(b) & a==b) | (is.na(a) & is.na(b))}
docend <- function() {replace(a, which(is.na(a)), Inf)==replace(b, which(is.na(b)), Inf)}
library(microbenchmark)
microbenchmark(mm(),akrun1(),cathG(),docend(),
unit='relative', times=100L)
# Unit: relative
# expr min lq mean median uq max neval
# mm() 1.000000 1.000000 1.000000 1.000000 1.000000 1.0000000 100
# akrun1() 1.667242 1.884185 1.815392 1.642581 1.765238 0.9973017 100
# cathG() 2.447168 2.449597 2.118306 2.201346 2.358105 1.1421577 100
# docend() 1.683817 1.950970 1.756481 1.745400 2.007889 1.2264461 100
Run Code Online (Sandbox Code Playgroud)
扩展 ==
由于最初的问题是真的要找到:
最简单的方法来获得
R的==登录有去无回NAs
这是一种我们定义新类的方法na_comparable.只有一个向量需要属于这个类,因为另一个向量将被强制转换.
na_comparable <- setClass("na_comparable", contains = "numeric")
`==.na_comparable` <- function(a,b){
x <- unclass(a) == unclass(b) # inefficient but I don't know how to force the default `==`
na_x <- which(is.na(x))
x[na_x] <- is.na(a[na_x]) & is.na(b[na_x])
x
}
`!=.na_comparable` <- Negate(`==.na_comparable`)
a <- na_comparable(a)
a == b
# [1] TRUE TRUE FALSE
b == a
# [1] TRUE TRUE FALSE
a != b
# [1] FALSE FALSE TRUE
b != a
# [1] FALSE FALSE TRUE
Run Code Online (Sandbox Code Playgroud)
在dplyr链中,它可以通过这种方式方便地使用:
data.frame(a=c(1,NA,3),b=c(1,NA,4)) %>%
mutate(a = na_comparable(a),
c = a==b,
d= a!=b)
# a b c d
# 1 1 1 TRUE FALSE
# 2 NA NA TRUE FALSE
# 3 3 4 FALSE TRUE
Run Code Online (Sandbox Code Playgroud)
通过这种方法,如果你需要更新的代码,以考虑NAs该缺席之前,你可能会用单个设置na_comparable呼叫,而不是改变你的初始数据或更换所有==有%==%向下行.
| 归档时间: |
|
| 查看次数: |
845 次 |
| 最近记录: |