This function matches food units in a data frame to a standard list of units. The function creates a unit key column in the data frame and uses it to match the food units to the standard list of units. The function can use an internal list of units or a user-provided list of units. The function can overwrite the original columns with the matched columns or create new columns for the matched units. The function returns the data frame with the matched units and a column indicating the source of the match.
Usage
match_food_units_v2(
data,
country,
survey,
unit_name_col,
unit_code_col,
matches_csv = NULL,
overwrite = FALSE
)
Arguments
- data
A data frame containing the food units to be matched.
- country
A character string indicating the country for which the food units are being matched.
- survey
A character string indicating the survey for which the food units are being matched.
- unit_name_col
A character string indicating the name of the column containing the food unit names in the data frame.
- unit_code_col
A character string indicating the name of the column containing the food unit codes in the data frame.
- matches_csv
A character string indicating the path to a CSV file containing the standard list of units. If NULL, the function uses an internal list of units.
- overwrite
A logical value indicating whether to overwrite the original columns with the matched columns (TRUE) or create new columns for the matched units (FALSE).
Examples
# Match food units in a data frame to a standard list of units
data <- data.frame(food = c("apple", "banana", "orange"), unit = c("kg", "lb", "g"))
match_food_units_v2(data, "MWI", "IHS5", "food", "unit")
#>
#> There are 3 out of 3 rows with unmatched units, which represents 100% of the data. The missing values are shown in the pop up view:
#>
#> food unit matched_food matched_unit unit_source
#> 1 apple kg <NA> <NA> NO-MATCH
#> 2 banana lb <NA> <NA> NO-MATCH
#> 3 orange g <NA> <NA> NO-MATCH