Special Values in R Programming: A Quick Guide

There are some special values in R Programming language, namely, these are NA, Inf, -inf, NaN, and NULL.

Special Values in R Programming Language

For numeric variables, several formalized special values are used. The calculations involving special values often result in special values. Regarding statistics, the real-world phenomenon should not include a special value. Therefore, it is desirable to handle special values before performing any statistical, especially inferential analysis. On the other hand, functions in R result in errors or warnings when a variable contains special values.

The NA values in R (NA stands for Not Available) represent the missing observations. A missing value may occur due to the non-response of the respondent or may arise when the vector size is expanded. For example,

v = c(1, 5, 6)
v[5] = 4
v

## Output
[1]  1  5  6 NA  4

To learn about how to handle missing values in R, see the article: Handling Missing Values in R

Inf and -Inf values in R represent a too-big number, which occurs during computation. Inf is for the positive number and -Inf is for the negative number (both represent the positive infinity, and negative infinity, respectively). Inf or -Inf also results when a value or variable is divided by 0. For example,

2 ^ 1024
## Output
[1] Inf

-2^1024

## Output
[1] -Inf

1/0

## Output
[1] Inf

-Inf + 1e10

## Output
[1] -Inf
Special Values in R programming Language

Sometimes a computation will produce a result that makes little sense. In such cases, R often returns NaN (Not a Number). For example,

Inf - Inf
NaN
0/0

## Output

In R, the Null object is represented by the symbol NULL. It is often used as an argument in functions to represent that no value was assigned to the argument. Additionally, some functions may return NULL. Note that the NULL is not the same as NA, Inf, -Inf, or NaN.

Getting Information about Special Values

Also, look at the str(), typeof(), and the length of Inf, -Inf, NA, NaN, and Null.

It is worth noting that, the special values in numeric variables indicate values that are not an element of the mathematical set of real numbers. One can use is.finite() function to determine whether the values are regular values or special values. is.finite() function only accepts vector objects. for example,

is.finite(c(1, Inf, NaN, NA))

A function can be written to deal with every numerical column in a data frame. For example,

special <- function(x){
    if (is.numeric(x)){
        return(!is.finite(x))
    }else {
        return (is.na(x))
    }
}

sapply(airquality, special)
Special values in R programming

The user defined special() function will test each column of the data frame object (airquality). The function will each special value if the object is numeric, otherwise it only checks for NA.

R FAQs: Special Values in R Programming

https://itfeature.com

https://gmstat.com

Leave a Reply

Discover more from R Language Frequently Asked Questions

Subscribe now to keep reading and get access to the full archive.

Continue reading