R/column_based_recode.R
column_based_recode.Rd
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
)
A data.frame object for which recoding is to be done.
Currently supports one of all_na or any_na to index rows that are either all NA or contain any NA.
Character. Name of column to get the original values from
Character New column name for the newly recoded values. Defaults to the same name if none is supplied.
The value to convert to `NA`. We can for instance change "n/a" to `NA` or any other value.
One of contains', 'starts_with' or 'ends_with'.
A character pattern to match
Defaults to FALSE. Patterns are case insensitive if TRUE
A `data.frame` object with target `NA` values replaced.
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