toLookupClass: Keys and Values to Lookup Structure

View source: R/lookup.R

toLookupClassR Documentation

Keys and Values to Lookup Structure

Description

Provide the mapping between keys and values in a structure of choice

Usage

toLookupClass(
  keys,
  values,
  class = c("data.frame.1", "data.frame.2", "list", "vector")[1]
)

Arguments

keys

vector of keys

values

vector of values

class

character string determining the class of the structure returned: "data.frame.1": data frame with the keys as column names and the values in the first row; "data.frame.2": data frame with keys in the first and values in the second column; "list": list with values as elements and keys as element names; "vector": named vector with values as elements and keys as names.

Value

object according to the chosen class. See description of the class argument.

Examples

keys <- c("A", "B", "C")
values <- c("Apple", "Banana", "Cherry")

fruits.df1 <- toLookupClass(keys, values)
fruits.df2 <- toLookupClass(keys, values, class = "data.frame.2")
fruits.list <- toLookupClass(keys, values, class = "list")
fruits.vector <- toLookupClass(keys, values, class = "vector")

# Note how you may use the results differently

fruits.df1$A
fruits.list$A
fruits.vector["A"]

fruits.df1[c("A", "C")]
fruits.list[c("A", "C")]
fruits.vector[c("A", "C")]


KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.