Type: | Package |
Title: | Functions Similar to VLOOKUP in Excel |
Version: | 1.1 |
Description: | Simple functions to lookup items in key-value pairs. See Mehta (2021) <doi:10.1007/978-1-4842-6613-7_6>. |
License: | MIT + file LICENSE |
URL: | https://kwstat.github.io/lookup/ |
BugReports: | https://github.com/kwstat/lookup/issues |
Suggests: | knitr, rmarkdown, testthat (≥ 3.0.0) |
VignetteBuilder: | knitr |
Config/testthat/edition: | 3 |
Encoding: | UTF-8 |
Language: | en-US |
RoxygenNote: | 7.3.2 |
NeedsCompilation: | no |
Packaged: | 2025-07-05 16:42:58 UTC; wrightkevi |
Author: | Kevin Wright |
Maintainer: | Kevin Wright <kw.stat@gmail.com> |
Repository: | CRAN |
Date/Publication: | 2025-07-05 19:50:03 UTC |
Lookup items in key-value pairs of vectors
Description
This is a simple wrapper to the match
function.
Usage
lookup(x, key, value, nomatch = NA)
Arguments
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. |
Details
Search for elements of x
in key
and return the corresponding
element of value
.
If no match is found, return nomatch
.
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.
Author(s)
Kevin Wright
Examples
# 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)
Lookup items in key-value dataframe similar to Excel's vlookup function
Description
This is a simple wrapper to the match
function.
Usage
vlookup(x, data, key = "key", value = "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. |
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
.
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.
Author(s)
Kevin Wright
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")