merge_and_simplify_core_values: Merge and Simplify Core Values

View source: R/ExtractNewValues.R

merge_and_simplify_core_valuesR Documentation

Merge and Simplify Core Values

Description

When extracting new values from the RHoMIS dataset, some variables appear in multiple columns. For example with units, we can have "crop_yield_units" and "crop_yield_units_other". These are all crop_yield units and we would like to ensure they are aggregated appropriately.

Usage

merge_and_simplify_core_values(
  list_of_unique_core_values,
  main_item,
  categories_to_merge
)

Arguments

list_of_unique_core_values

A nested list of new values from a RHoMIS survey

main_item

The primary item that you want to merge. In "crop_yield_units" and "crop_yield_units_other", the main item would be "crop_yield_units"

categories_to_merge

A named list of the categories which go together, please see example

Details

This is heavily linked to the "extract_new_core_units" function.

Rpackage file: ExtractNewValues.R

Examples


categories_to_merge <- list(
  crop_name = c("crop_name"),
  livestock_name = c("livestock_name"),
  crop_yield_units = c("crop_yield_units_other"),
  crop_sold_price_quantityunits = c("crop_price_quantityunits_other"),
  unitland = c(
    "areaunits_other",
    "unitland_owned",
    "unitland_rentin",
    "unitland_rentout"
  ),
  milk_units = c("milk_amount_units_other"),
  milk_sold_price_timeunits = c("milk_amount_time_units_other"),
  bees_honey_production_units = c("bees_honey_production_units_other"),
  eggs_units = c("eggs_amount_units_other"),
  eggs_sold_price_timeunits = c("eggs_sold_price_timeunits_other"),
  fertiliser_units = c("fertiliser_units_other")
)

list_of_unique_core_values <- c(list(
  crop_sold_price_quantityunits = c(
    "cropPriceA",
    "cropPriceB",
    "cropPriceC"
  ),
  crop_price_quantityunits_other = c(
    "cropPriceD",
    "cropPriceE",
    "cropPriceF"
  ),
  random_unimportant_column = c("bla", "bla", "bla")
))

main_item <- "crop_sold_price_quantityunits"
categories_to_merge <- list(
  crop_name = c("crop_name"),
  livestock_name = c("livestock_name"),
  crop_yield_units = c("crop_yield_units_other"),
  crop_sold_price_quantityunits = c("crop_price_quantityunits_other"),
  unitland = c(
    "areaunits_other",
    "unitland_owned",
    "unitland_rentin",
    "unitland_rentout"
  ),
  milk_units = c("milk_amount_units_other"),
  milk_sold_price_timeunits = c("milk_amount_time_units_other"),
  bees_honey_production_units = c("bees_honey_production_units_other"),
  eggs_units = c("eggs_amount_units_other"),
  eggs_sold_price_timeunits = c("eggs_sold_price_timeunits_other"),
  fertiliser_units = c("fertiliser_units_other")
)

expected_result <- c(
  "cropPriceA",
  "cropPriceB",
  "cropPriceC",
  "cropPriceD",
  "cropPriceE",
  "cropPriceF"
)
merge_and_simplify_core_values(
  list_of_unique_core_values,
  main_item,
  categories_to_merge
)

l-gorman/rhomis-R-package documentation built on Nov. 8, 2023, 6:46 a.m.