How to check against a value range in a mapping table?

Viewed 90

Inputs:

library(dplyr)
A <- data_frame(Type=c("A","A","B"),R=c(3, 7, 12), Value = c(120, 130,300))
A
   Type     R Value
  (chr) (dbl) (dbl)
1     A     3   120
2     A     7   130
3     B    12   300

Mapping dataset:

Mapping <- data_frame(Type=c("A","A","A","B","B","B"), Rmin =     c(1,6,11,1,6,11), Rmax = c(5,10,15,5,10,15), Threshold = c(50,100,200,50,200,300))
Mapping

   Type  Rmin  Rmax Threshold
  (chr) (dbl) (dbl)     (dbl)
1     A     1     5        50
2     A     6    10       100
3     A    11    15       200
4     B     1     5        50
5     B     6    10       200
6     B    11    15       300

Output are expected to get with following step.

  1. With the same Type value among A and Mapping, find the threshold value for the entry that has R >= Rmin and R <= Rmax. For example, for row 1 table in A, the threhold should be 50 (1<=3<=5, and type A).
  2. isAbove value is obtained by assigning isAbove <- 120>=50

As a result, the expected output is:

B <-  data_frame(Type=c("A","A","B"),R=c(3, 7, 12), Value = c(60, 90,250), isAbove = c(T,F,F))
 B

    Type     R Value isAbove
   (chr) (dbl) (dbl)   (lgl)
 1     A     3    120    TRUE
 2     A     7    130   TRUE
 3     B    12    300  FALSE
1 Answers
Related