Learn R Programming

expss (version 0.8.7)

na_if: Replace certain values with NA

Description

There are following options for value:

  • vector Vector of values which should be replaced with NA in x.

  • logical vector/matrix/data.frame NA's will be set in places where value is TRUE. value will be recycled if needed.

  • function NA's will be set in places where value(x) is TRUE. Function will be applied columnwise. Additionally, there are special functions for common cases of comparison. For example na_if(my_var, gt(98)) will replace all values which are greater than 98 in my_var with NA. For detailed description of special functions see criteria

mis_val is an alias for the na_if with absolutely the same functionality.

Usage

na_if(x, value)

na_if(x) <- value

x %na_if% value

mis_val(x, value)

mis_val(x) <- value

x %mis_val% value

Arguments

x

vector/matrix/data.frame/list

value

vector/matrix/data.frame/function

Value

x with NA's instead of value

See Also

For reverse operation see if_na, if_val for more general recodings.

Examples

Run this code
# NOT RUN {
a = c(1:5, 99)

# 99 to NA
na_if(a, 99)    # c(1:5, NA)

a %na_if% 99    # same result

# values which greater than 5 to NA
na_if(a, gt(5)) # c(1:5, NA)

set.seed(123)
dfs = data.frame(
      a = c("bad value", "bad value", "good value", "good value", "good value"),
      b = runif(5)
)

# rows with 'bad value' will be filled with NA
# logical argument and recycling by columns
na_if(dfs, dfs$a=="bad value")

a = rnorm(50)
# values greater than 1 or less than -1 will be set to NA
# special functions usage
na_if(a, lt(-1) | gt(1))

# values inside [-1, 1] to NA
na_if(a, -1 %thru% 1)
# }

Run the code above in your browser using DataLab