[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**One Tip Per Day**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

We usually see NA and NaN in R. What’s the difference between them?

Here a good post for that topic:

http://stats.stackexchange.com/questions/5686/what-is-the-difference-between-nan-and-na

In summary here:

- NaN (“Not a Number”) means 0/0
- NA (“Not Available”) is generally interpreted as a missing value and has various forms – NA_integer_, NA_real_, etc.
- Therefore, NaN ≠ NA and there is a need for NaN and NA.
- is.na() returns TRUE for both NA and NaN, however is.nan() return TRUE for NaN (0/0) and FALSE for NA.
- As the elements of an atomic vector must be of the same type
**there are multiple types of NA values**. There is one case where this is particularly important to the user.**The default type of NA is logical**, unless coerced to some other type, so the appearance of a missing value may trigger logical rather than numeric indexing. Numeric and logical calculations with NA generally return NA. In cases where the result of the operation would be the same for all possible values the NA could take, the operation may return this value.**In particular, ‘FALSE & NA’ is FALSE, ‘TRUE | NA’ is TRUE. NA is not equal to any other value or to itself; testing for NA is done using is.na.**However, an NA value will match another NA value in match. - Numeric calculations whose result is undefined, such as ‘0/0’, produce the value NaN. This exists only in the double type and for real or imaginary components of the complex type. The function
**is.nan is provided to check specifically for NaN, is.na also returns TRUE for NaN**. Coercing NaN to logical or integer type gives an NA of the appropriate type, but coercion to character gives the string “NaN”. NaN values are incomparable so tests of equality or collation involving NaN will result in NA. They are regarded as matching any NaN value (and no other value, not even NA) by match. The NA of character type is as from R 1.5.0 distinct from the string “NA”.**Programmers who need to specify an explicit string NA should use ‘as.character(NA)’ rather than “NA”, or set elements to NA using is.na<-.**As from R 2.5.0 there are constants NA_integer_, NA_real_, NA_complex_ and NA_ character_ which will generate (in the parser) an NA value of the appropriate type, and will be used in deparsing when it is not otherwise possible to identify the type of an NA (and the control options ask for this to be done). There is no NA value for raw vectors.

To

**leave a comment**for the author, please follow the link and comment on their blog:**One Tip Per Day**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.