Learn R Programming

mde (version 0.3.2)

column_based_recode: Conditionally Recode NA values based on other Columns

Description

Recode NA as based on Other Columns

Usage

column_based_recode(
  df,
  criterion = "all_na",
  values_from = NULL,
  values_to = NULL,
  value = 0,
  pattern_type = "contains",
  pattern = "Solar",
  case_sensitive = FALSE
)

Arguments

df

A data.frame object for which recoding is to be done.

criterion

Currently supports one of all_na or any_na to index rows that are either all NA or contain any NA.

values_from

Character. Name of column to get the original values from

values_to

Character New column name for the newly recoded values. Defaults to the same name if none is supplied.

value

The value to convert to `NA`. We can for instance change "n/a" to `NA` or any other value.

pattern_type

One of contains', 'starts_with' or 'ends_with'.

pattern

A character pattern to match

case_sensitive

Defaults to FALSE. Patterns are case insensitive if TRUE

Value

A `data.frame` object with target `NA` values replaced.

Examples

Run this code
# NOT RUN {
df <- structure(list(id = 40:43, v1 = c(NA, 1L, 1L, 1L), v2 = c(NA, 1L, 1L, 1L),
v3 = c(NA, 2L, NA, 1L),
test = c(1L, 2L, 1L, 3L)), class = "data.frame", row.names = c(NA, -4L))
# recode test as 0 if all NA, return test otherwise
column_based_recode(df,values_from = "test", pattern_type = "starts_with", pattern="v")
# }

Run the code above in your browser using DataLab