switch.items: Match values for vector items

Description Usage Arguments Examples

Description

Match each item in a vector to a source column and return the corresponding target column as a vector. The output of this function is identical to: as.character( sapply(lookup, FUN = function(x) ref[ref[,source_index] == x, target_index]) ) but faster for large datasets.

Usage

1
2
switch.items(lookup, ref, source_index, target_index,
  multiple_targets = "omit", no_targets = "omit")

Arguments

lookup

A vector containing values to replace

ref

The reference dataframe containing the source items to match against and the target items to be returned

source_index

The index of the column containing the items to match against

target_index

The index of the column containing the items to return

multiple_targets

Action in case of 1:many mapping between source and target items. Valid options are "omit" (default) to remove the item, "keep" to keep the lookup item, "first" to use the first target item or "last" to use the last target item.

no_targets

Action if no matching value is found in source index. Valid options are "omit" (default) or "keep" to keep the lookup item.

Examples

1
2
3
4
5
# Example using a random dataset with categorical data:
devtools::install_github('martinry/randomcds')
df <- randomcds::cds(50, 4) # Generate a dataframe with 50 rows and 4 columns
rv <- df[sample.int(50, 10),1] # Create a vector containing a subset (10 items) from the first column in df
rv2 <- qob::switch.items(rv, df, 1, 3) # For each item in rv, find the item in df[,1] and return the corresponding item in df[,3]

martinry/qob documentation built on Aug. 30, 2019, 1:14 p.m.