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 thevalues
in the first row; "data.frame.2": data frame withkeys
in the first andvalues
in the second column; "list": list withvalues
as elements andkeys
as element names; "vector": named vector withvalues
as elements andkeys
as names.
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"