Skip to content

This function enriches a data frame with additional data based on New Zealand electorate names. It dynamically adds specified data types such as regions or electorate types (i.e., "General" or "Maori") as new columns based on the specified output type. It utilises internal datasets region and type, which map electorates to their corresponding regions or types.

The function validates the input data frame and specified column. It converts factor columns to character to ensure correct matching. Warnings are issued if the specified column contains NA values, or if any electorates cannot be matched. A warning and the original data frame are returned if none of the electorates can be matched.

Usage

add_data(data, electorateCol = "Electorate", output = c("region", "type"))

Arguments

data

A data frame containing New Zealand electorate information.

electorateCol

Name of the column in data containing electorate names. Defaults to "Electorate".

output

Type of additional data to append. The options are: - "region": Adds a "Region" column with New Zealand region. - "type": Adds an "Electorate_Type" column with electorate types. Default is "region".

Value

A data frame identical to data but with the additional column appended. In case of issues like unmatched electorates, the original data frame is returned with a warning.

Examples

if (FALSE) {
  data(majority)
  df_region <- add_data(majority, output = "region")
  df_types <- add_data(majority, output = "type")
}