Skip to contents

Vlookup replace using a lookup column and reference table

Usage

vlookup_from_ref(base, lookup_col, lookup_ref, value_col)

Arguments

base

vector of starting values. Replacements will be made in this vector before it is returned.

lookup_col

vector of values with same length as base that will be matched to lookup_ref to deteremine the replacement indices

lookup_ref

vector of reference values, which lookup_col will be matched to in order to determine replacement values.

value_col

vector of replacement values with same length as lookup_ref

Value

vector

Examples

ref <- tibble::tibble(lookup = letters[1:5], value = 1:5)
original <- tibble::tibble(lookup = letters[1:20], base = runif(20, min = 6, max = 20))
dplyr::mutate(original, base = vlookup_from_ref(base, lookup, ref$lookup, ref$value))
#> # A tibble: 20 × 2
#>    lookup  base
#>    <chr>  <dbl>
#>  1 a       1   
#>  2 b       2   
#>  3 c       3   
#>  4 d       4   
#>  5 e       5   
#>  6 f      17.9 
#>  7 g       7.49
#>  8 h      12.8 
#>  9 i       9.46
#> 10 j      15.6 
#> 11 k       8.29
#> 12 l      19.3 
#> 13 m      10.5 
#> 14 n      11.1 
#> 15 o      18.4 
#> 16 p      17.6 
#> 17 q       7.41
#> 18 r      18.7 
#> 19 s      16.8 
#> 20 t      11.4