| Title: | Functions Similar to VLOOKUP in Excel |
|---|---|
| Description: | Simple functions to lookup items in key-value pairs. See Mehta (2021) <doi:10.1007/978-1-4842-6613-7_6>. |
| Authors: | Kevin Wright [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-0617-8673>) |
| Maintainer: | Kevin Wright <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0 |
| Built: | 2026-05-12 05:43:54 UTC |
| Source: | https://github.com/kwstat/lookup |
This is a simple wrapper to the match function.
lookup(x, key, value, nomatch = NA)lookup(x, key, value, nomatch = NA)
x |
Vector of items to lookup in key-value pairs. |
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. |
Search for elements of x in key and return the corresponding
element of value.
If no match is found, return nomatch.
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.
Kevin Wright
# Example 1. A and B have different factor levels A <- factor(c("A","E","F")) B <- factor(c("E","F","G")) v <- c(4,2,0) lookup(A,B,v) # Example 2. Merge treatment means back into the raw data dat <- data.frame(Trt = rep(LETTERS[1:5],2), x=round(rnorm(10),2)) # Treatment B is missing all values, treatment D missing one value dat$x[dat$Trt=="B"] <- NA dat$x[4] <- NA # Calculate treatment means TrtMean <- tapply(dat$x, dat$Trt, mean, na.rm=TRUE) TrtMean # Merge the means into the original data dat$TrtMean <- lookup(dat$Trt, names(TrtMean), TrtMean)# Example 1. A and B have different factor levels A <- factor(c("A","E","F")) B <- factor(c("E","F","G")) v <- c(4,2,0) lookup(A,B,v) # Example 2. Merge treatment means back into the raw data dat <- data.frame(Trt = rep(LETTERS[1:5],2), x=round(rnorm(10),2)) # Treatment B is missing all values, treatment D missing one value dat$x[dat$Trt=="B"] <- NA dat$x[4] <- NA # Calculate treatment means TrtMean <- tapply(dat$x, dat$Trt, mean, na.rm=TRUE) TrtMean # Merge the means into the original data dat$TrtMean <- lookup(dat$Trt, names(TrtMean), TrtMean)
This is a simple wrapper to the match function.
vlookup(x, data, key, value, nomatch = NA)vlookup(x, data, key, value, nomatch = NA)
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. |
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.
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.
Kevin Wright
# 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")# 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")