Last active
July 6, 2017 18:32
-
-
Save bgall/1de4e8e741491ed478554b3c5259f8d7 to your computer and use it in GitHub Desktop.
Demonstrates the idiosyncratic behavior of NA values in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The below demonstrates the madness of R's treatment of NA values. | |
# Some examples taken from https://stackoverflow.com/questions/25100974/na-matches-na-but-is-not-equal-to-na-why/25101796 | |
# Logical examples | |
NA %in% NA | |
# [1] TRUE | |
NA == NA | |
# [1] NA | |
NA | TRUE | |
# [1] TRUE | |
NA_real_ | TRUE | |
# [1] TRUE | |
NA_integer_ | TRUE | |
# [1] TRUE | |
NA | FALSE | |
# [1] NA | |
NA_real_ | FALSE | |
# [1] NA | |
NA_integer_ | FALSE | |
# [1] NA | |
TRUE | paste(NA) | |
# Error in TRUE | paste(NA) : | |
# operations are possible only for numeric, logical or complex types | |
# Matching examples | |
match(NA, NA) | |
# [1] 1 | |
match(NA, NA_real_) | |
# [1] 1 | |
match(NA_character_, NA_real_) | |
# [1] 1 | |
match(paste(NA), NA) | |
# [1] NA | |
gsub("NA", "", NA) | |
# [1] NA | |
gsub("NA", "", paste(NA)) | |
# [1] "" | |
is.na(NA) | |
# [1] TRUE | |
is.na(paste(NA)) | |
# [1] FALSE | |
# Other examples | |
identical(NA, NA) | |
# [1] TRUE | |
eval(NA) | |
# [1] NA | |
is.na(eval(NA)) | |
# [1] TRUE | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment