meta_apply | R Documentation |
To work with data from different sources a normilization of naming is necessary. This function allows flexible renaming/recoding of the input data based on a second table. In short following operation is performed:
data$data_dest = meta[meta$meta_key == data$data_src]$meta_val
For handling missing values for meta_key in meta there are different modes:
strict: stop execution
drop: drop rows from data
keep: keep the values in data
replace: use the mapping provided in the argument replacement (named vector/list)
The function is quiet chatty and reports which values are dropped, kept or replaced.
meta_apply(
data,
meta,
data_src,
data_dest,
meta_key,
meta_val,
mode = "strict",
replacements = NULL
)
data |
Input data as tibble |
meta |
Lookup table as tibble |
data_src |
Name of the column in data used to lookup in meta |
data_dest |
Name of the column to save the result in data |
meta_key |
Name of the column in meta to match against data$data_src |
meta_val |
Name of the column containing the replacement value in meta |
mode |
One of "strict", "drop", "keep", "replace". Default "strict" |
replacements |
Named vector/list with missing values in meta$meta_key or to overwrite specific mappings |
transformed data
Under the hood the heavy lifting is done by dplyr::recode()
.
meta_fn <- system.file("extdata", "meta_smn.rds",
package = "rOstluft.data", mustWork = TRUE)
meta <- readRDS(meta_fn)
tibble::glimpse(meta)
fn <- system.file("extdata", "smn.txt", package = "rOstluft.data", mustWork = TRUE)
data <- read_smn(fn, na.rm = FALSE)
data <- dplyr::arrange(data, .data$starttime)
data
# data contains no units, cryptic SwissMetNet parameter names and abbreviations for site.
# And the meta data for parameter rre150z0 is missing. Perfect!
# too lazy to update meta, add unit mapping based on SwissMetNet parameter names
# and we want to overwrite the mapping for dkl010z0 anyway => use replace
res <- meta_apply(data, meta, "parameter", "unit", "parameter_original", "unit",
mode = "replace",replacements = list(rre150z0 = "unit1", dkl010z0 = "unit2"))
res
# rename the SwissMetNet Parameters, still no mapping for rre150z0, we aren't
# interested in the data and drop it
res2 <- meta_apply(res, meta, "parameter", "parameter",
"parameter_original", "parameter", mode = "drop")
res2
# or we keep it
res <- meta_apply(res, meta, "parameter", "parameter",
"parameter_original", "parameter", mode = "keep")
res
# rename the site abbreviation to the site name, strict should work
res <- meta_apply(res, meta, "site", "site", "site_short", "site")
res
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.