Skip to contents

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
#> [1] "Apple"
fruits.list$A
#> [1] "Apple"
fruits.vector["A"]
#>       A 
#> "Apple" 

fruits.df1[c("A", "C")]
#>       A      C
#> 1 Apple Cherry
fruits.list[c("A", "C")]
#> $A
#> [1] "Apple"
#> 
#> $C
#> [1] "Cherry"
#> 
fruits.vector[c("A", "C")]
#>        A        C 
#>  "Apple" "Cherry"