本文介绍了与NA匹配的值-缺少值-使用mutate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有些困惑。是否有比下面更好的方法来进行值匹配,将NA视为 mutate 内的真实值?

I am somewhat stuck. Is there a better way than the below to do value matching considering NAs as "real values" within mutate?

library(dplyr)

data_foo <- data.frame(A= c(1:2, NA, 4, NA), B = c(1, 3, NA, NA, 4))

不是所需的输出:

data_foo %>% mutate(irr = A==B)

#>    A  B   irr
#> 1  1  1  TRUE
#> 2  2  3 FALSE
#> 3 NA NA    NA
#> 4  4 NA    NA
#> 5 NA  4    NA

data_foo %>% rowwise() %>% mutate(irr = A%in%B)

#> Source: local data frame [5 x 3]
#> Groups: <by row>
#> 
#> # A tibble: 5 x 3
#>       A     B irr  
#>   <dbl> <dbl> <lgl>
#> 1     1     1 TRUE 
#> 2     2     3 FALSE
#> 3    NA    NA FALSE
#> 4     4    NA FALSE
#> 5    NA     4 FALSE

所需的输出:下面显示了所需的列, irr 。我正在使用这种麻烦的辅助专栏。有更短的方法吗?

Desired output: The below shows the desired column, irr. I am using this somewhat cumbersome helper columns. Is there a shorter way?

data_foo %>% 
  mutate(NA_A = is.na(A), 
         NA_B = is.na(B), 
         irr = if_else(is.na(A)|is.na(B), NA_A == NA_B, A == B))

#>    A  B  NA_A  NA_B   irr
#> 1  1  1 FALSE FALSE  TRUE
#> 2  2  3 FALSE FALSE FALSE
#> 3 NA NA  TRUE  TRUE  TRUE
#> 4  4 NA FALSE  TRUE FALSE
#> 5 NA  4  TRUE FALSE FALSE


推荐答案

也许比 akrun的答案?

以下两种方式中的任何一种都会产生预期的结果。请注意, as.character 不会这样做,因为 as.character(NA)的返回值为 NA_character _

Maybe simpler than akrun's answer?
Any of the two ways below will produce the expected result. Note that as.character won't do it, because the return value of as.character(NA) is NA_character_.

data_foo %>%
  mutate(irr = paste(A) == paste(B))

data_foo %>%
  mutate(irr = sQuote(A) == sQuote(B))

#Source: local data frame [5 x 3]
#Groups: <by row>
#
## A tibble: 5 x 3
#      A     B irr  
#  <dbl> <dbl> <lgl>
#1     1     1 TRUE 
#2     2     3 FALSE
#3    NA    NA TRUE 
#4     4    NA FALSE
#5    NA     4 FALSE

编辑。


  1. 在下面的注释之后,我更新了代码,现在它遵循akrun的建议。

  2. 在。我使用另一种类似的方法来解决问题的问题。

  1. Following the comments below I have updated the code and it now follows akrun's suggestion.
  2. There is also the excellent idea in tmfmnk's answer. I use a similar one in yet another way of solving the question's problem.

all.equal的文档表示

尽管没有如果 mutate 中的if 表达式,我相信它比相同更稳定,并且具有如果所比较的值相等(实际上),则效果相同。

Though there is no if expression in mutate, I believe that it is more stable than identical and has the same effect if the values being compared are (sort of/in fact) equal.

data_foo %>%
  mutate(irr = isTRUE(all.equal(A, B)))

这篇关于与NA匹配的值-缺少值-使用mutate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 11:38