Recode NA as based on Other Columns

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

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")
#>   id v1 v2 v3 test
#> 1 40 NA NA NA    0
#> 2 41  1  1  2    2
#> 3 42  1  1 NA    1
#> 4 43  1  1  1    3