Pull value that is between a range (2 columns)

Viewed 23

I have two columns in my dataset - one with the lowest value of a range and one with the highest value of a range. A value can be entered by a numeric input in the shiny app. Using a reactive filter, I want to pull the value from the dataset that is between the range. For example, if I type 27 in the numeric input I want to pull the value (0.3) that is between 14 and 35.

age_lower_border age_higher_border value
5 13 0.5
14 35 0.3
36 99 0.1

I tried using <= and >=:

risk_infection_nv <- reactive({
       basis_nutzen1 %>%  filter(
           age_lower_border <= as.numeric(input$age) &
           age_higher_border >= as.numeric(input$age) & 
           contact == as.numeric(input$contact)) %>% 
           dplyr::pull(value)

But I only get errors like 'list' object cannot be coerced to type 'integer'.

Thank you for any help!

1 Answers

Use two filters.

n = 27

tribble(~"age_lower_border",    ~"age_higher_border",   ~"value",
        5,  13, 0.5,
        14, 35, 0.3,
        36, 99, 0.1) %>% 
  dplyr::filter(age_lower_border < n) %>% 
  dplyr::filter(age_higher_border > n) %>% 
  dplyr::pull(value)

Output

[1] 0.3
Related