This is a simple wrapper to the
lookup(x, key, value, nomatch = NA)
Vector of items to lookup in key-value pairs.
Vector of keys that are searched.
Vector of values to be returned.
The value to be returned in the case when no match is found. Note that it is coerced to integer.
A vector the same length as
x, but containing the values
x[i] is equal to
key[j], then the
value returned in the ith position of the vector is
If no match is found,
NA is returned.
Search for elements of
key and return the corresponding
If no match is found, return
# 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)#>  NA 4 2# 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 <- NA # Calculate treatment means TrtMean <- tapply(dat$x, dat$Trt, mean, na.rm=TRUE) TrtMean#> A B C D E #> -0.125 NaN -1.345 -0.240 0.170# Merge the means into the original data dat$TrtMean <- lookup(dat$Trt, names(TrtMean), TrtMean)