Learn R Programming

datapackage (version 0.2.1)

dp_apply_schema: Convert columns of data.frame to their correct types using table schema

Description

Convert columns of data.frame to their correct types using table schema

Usage

dp_apply_schema(
  dta,
  resource,
  convert_categories = c("no", "to_factor", "to_code"),
  reorder = TRUE,
  ...
)

Value

Returns a copy of the input data.frame with columns modified to match the types given in de table schema. When reorder = TRUE columns are put in the same order as in the data resource with fields not in the data resource put at the back of the data.frame.

Arguments

dta

a data.frame or data.table.

resource

an object with the Data Resource of the data set.

convert_categories

how to handle columns for which the field descriptor has a categories property. This should either be the strings "no", "to_factor", "to_code", the name of a function or a function. When equal to "no" the field is returned as is; when equal to "to_factor" each column is transformed using dp_to_factor; when equal to "to_code" each column is transformed using dp_to_code. In other cased the function is called with the column as its first parameter and warn = FALSE as its second argument. The result of this function call is added to the resulting data set.

reorder

put the columns in the same order as the fields in the data resource.

...

additional arguments are passed on to the dp_to_<fieldtype> functions (e.g. dp_to_number).

Details

Converts each column in dta to the correct R-type using the type information in the table schema. For example, if the original column type in dta is a character vector and the table schema specifies that the field is of type number, the column is converted to numeric using the decimal separator and thousands separator specified in the field descriptor (or default values for these if not).

See Also

This function calls conversion functions for each of the columns, see dp_to_number, dp_to_boolean, dp_to_integer, dp_to_date. dp_to_datetime, dp_to_yearmonth, and dp_to_string.