This is a simple wrapper to the match function.
Usage
vlookup(x, data, key, value, nomatch = NA)
Arguments
- x
Vector of items to lookup in key-value pairs.
- data
Dataframe containing key-value columns.
- key
Vector of keys that are searched.
- value
Vector of values to be returned.
- nomatch
The value to be returned in the case when no match is
found. Note that it is coerced to integer.
Value
A vector the same length as x, but containing the values
of value. If x[i] is equal to key[j], then the
value returned in the ith position of the vector is value[j].
If no match is found, NA is returned.
Details
Search for elements of x in dataframe data,
column key, and return the corresponding element of column
value.
If no match is found, return nomatch.
Examples
# Example 1. A and B have different factor levels
A <- factor(c("A","E","F"))
dat <- data.frame(trt = factor(c("E","F","G")),
val = c(4,2,0))
vlookup(A,dat, "trt", "val")
#> [1] NA 4 2