Skip to contents

A convenience function to easily look for values in a data frame.

Usage

look_up(data, ..., bin = FALSE, value = "value")

Arguments

data

A reference data frame.

...

Individual characteristics, should be named like the columns of data.

bin

Either logical: should all numeric variable be binned, or character vector giving the names of variables to bin (see examples).

value

The value to extract from the reference data frame.

Value

A vector of values, same length as ....

Details

This function is mostly used to extract population informations (such as mortality rates), given some individual characteristics.

If binning is activated, numeric individual characteristics are matched to the corresponding reference value that is directly inferior.

Examples

tempdf <- expand.grid(arg1 = c("A", "B", "C"), arg2 = 1:4, arg3 = 1:5)
tempdf$value <- 1:60

look_up(
  data = tempdf,
  value = "value",
  arg1 = c("A", "B", "C", "B", "A"),
  arg2 = c(1, 1, 3.2, 3.0, 5), 
  arg3 = c(-1, 1, 1, 2, 3)
)
#> Warning: Some values were not found, returning missing data:
#> arguments to look_up:
#> arg1 : A, C, A
#> arg2 : 1, 3.2, 5
#> arg3 : -1, 1, 3
#> [1] NA  2 NA 20 NA

# binning doesnt catch values lesser than the smaller
# reference value
look_up(
  data = tempdf,
  value = "value",
  arg1 = c("A", "B", "C", "B", "A"),
  arg2 = c(1, 1, 3.2, 3.0, 5), 
  arg3 = c(-1, 1, 1, 2, 3),
  bin = TRUE
)
#> Warning: Some values were not found, returning missing data:
#> arguments to look_up:
#> arg1 : A
#> arg2 : [1,2)
#> arg3 : NA
#> [1] NA  2  9 20 34
# bin can alos be given as a charater vector
# to avoid binning all numeric variables
look_up(
  data = tempdf,
  value = "value",
  arg1 = c("A", "B", "C", "B", "A"),
  arg2 = c(1, 1, 3.2, 3.0, 5), 
  arg3 = c(-1, 1, 1, 2, 3),
  bin = c("arg2")
)
#> Warning: Some values were not found, returning missing data:
#> arguments to look_up:
#> arg1 : A
#> arg2 : [1,2)
#> arg3 : -1
#> [1] NA  2  9 20 34

age_related_df <- data.frame(age = 10 * 0:9, decade = 1:10)

look_up(age_related_df, age = c(0, 10, 20), value = "decade")
#> [1] 1 2 3

# binning might help in the situation
look_up(age_related_df, age = c(5, 15, 23.5), 
        value = "decade")
#> Warning: Some values were not found, returning missing data:
#> arguments to look_up:
#> age : 5, 15, 23.5
#> [1] NA NA NA
look_up(age_related_df, age = c(5, 15, 23.5), 
        value = "decade", bin = TRUE)
#> [1] 1 2 3