Learn R Programming

datawizard (version 0.4.0)

convert_na_to: Replace missing values in a variable or a dataframe.

Description

Replace missing values in a variable or a dataframe.

Usage

convert_na_to(x, ...)

# S3 method for numeric convert_na_to(x, replacement = NULL, verbose = TRUE, ...)

# S3 method for character convert_na_to(x, replacement = NULL, verbose = TRUE, ...)

# S3 method for data.frame convert_na_to( x, replace_num = NULL, replace_char = NULL, replace_fac = NULL, select = NULL, exclude = NULL, ignore_case = FALSE, verbose = TRUE, ... )

Arguments

x

A numeric, factor, or character vector, or a data frame.

...

Not used.

replacement

Numeric or character value that will be used to replace NA.

verbose

Toggle warnings.

replace_num

Value to replace NA when variable is of type numeric.

replace_char

Value to replace NA when variable is of type character.

replace_fac

Value to replace NA when variable is of type factor.

select

Variables that will be included when performing the required tasks. Can be either

  • a variable specified as a literal variable name (e.g., column_name),

  • a string with the variable name (e.g., "column_name"), or a character vector of variable names (e.g., c("col1", "col2", "col3")),

  • a formula with variable names (e.g., ~column_1 + column_2),

  • a vector of positive integers, giving the positions counting from the left (e.g. 1 or c(1, 3, 5)),

  • a vector of negative integers, giving the positions counting from the right (e.g., -1 or -1:-3),

  • or one of the following select-helpers: starts_with(""), ends_with(""), contains(""), a range using : or regex("").

If NULL, selects all columns.

exclude

See select, however, column names matched by the pattern from exclude will be excluded instead of selected. If NULL (the default), excludes no columns.

ignore_case

Logical, if TRUE and when one of the select-helpers or a regular expression is used in select, ignores lower/upper case in the search pattern when matching against variable names.

Value

x, where NA values are replaced by replacement.

Examples

Run this code
# NOT RUN {
# Convert NA to 0 in a numeric vector
convert_na_to(
  c(9, 3, NA, 2, 3, 1, NA, 8),
  replacement = 0
)

# Convert NA to "missing" in a character vector
convert_na_to(
  c("a", NA, "d", "z", NA, "t"),
  replacement = "missing"
)

### For dataframes

test_df <- data.frame(
  x = c(1, 2, NA),
  x2 = c(4, 5, NA),
  y = c("a", "b", NA)
)

# Convert all NA to 0 in numeric variables, and all NA to "missing" in
# character variables
convert_na_to(
  test_df,
  replace_num = 0,
  replace_char = "missing"
)

# Convert a specific variable in the dataframe
convert_na_to(
  test_df,
  replace_num = 0,
  replace_char = "missing",
  select = "x"
)

# Convert all variables starting with "x"
convert_na_to(
  test_df,
  replace_num = 0,
  replace_char = "missing",
  select = starts_with("x")
)

# Convert NA to 1 in variable 'x2' and to 0 in all other numeric
# variables
convert_na_to(
  test_df,
  replace_num = 0,
  select = list(x2 = 1)
)

# }

Run the code above in your browser using DataLab