For a dataset's custom_R_code, on data with one column per trait. Where original_trait holds one of original_values, the matching element of values_for_new_trait is written to new_trait, and the value in original_trait is replaced by the matching element of values_to_keep. Use it when a source records in one column information that belongs to more than one trait – a growth form column that also says a plant is an epiphyte, say.

move_values_to_new_trait(
  data,
  original_trait,
  new_trait,
  original_values,
  values_for_new_trait,
  values_to_keep,
  blank_to_na = FALSE,
  to_lower = FALSE
)

Arguments

data

Data frame of the dataset, typically data.csv as read by the build

original_trait

Name of the column holding the values to move

new_trait

Name of the column to write the moved values to. Created if data has no such column

original_values

Values of original_trait to move

values_for_new_trait

Values written to new_trait, one per element of original_values. A single value is used for all of them

values_to_keep

Values left in original_trait, one per element of original_values: the same value to leave it as it was, another to rewrite it, or "" or NA to clear it. A single value is used for all of them

blank_to_na

Convert "" in original_trait to NA once the values have been moved? Use it with values_to_keep = "": the build treats "" as a value rather than as missing, and reports it among the excluded data. Default FALSE.

to_lower

Lower-case original_trait before matching, and match original_values ignoring case? values_to_keep are lower-cased too, so a value one of them writes is matched ignoring case by a later element of original_values. The build lower-cases trait values after custom_R_code runs in any case, so for a trait column this changes which values match rather than the values stored. Default FALSE.

Value

data, with values moved and new_trait added if it was absent

Details

new_trait is created, filled with NA, if data has no such column, so there is no need to create it first with mutate(). Name an existing column to add values to it instead: rows not matched keep what they held.

original_values are matched exactly and in the order given, each against the data as the one before left it, so a value written to original_trait by values_to_keep can be matched by a later element of original_values. NA never matches, and rows not matched are left unchanged.

See also

move_values_to_new_trait_long() for data with one row per trait value

Examples

data <- tibble::tibble(
  growth_form = c("shrub", "Epiphyte", "herb", "epiphytic herb", NA)
)

# Record epiphytes as a plant_growth_substrate, leaving the growth form
# where there is one
data %>%
  move_values_to_new_trait(
    "growth_form", "plant_growth_substrate",
    c("epiphyte", "epiphytic herb"), "epiphyte", c("", "herb"),
    blank_to_na = TRUE, to_lower = TRUE
  )
#> # A tibble: 5 × 2
#>   growth_form plant_growth_substrate
#>   <chr>       <chr>                 
#> 1 shrub       NA                    
#> 2 NA          epiphyte              
#> 3 herb        NA                    
#> 4 herb        epiphyte              
#> 5 NA          NA