0

I'm looking for a search function in R that will allow me to allocate values within ranges, or the equivalent of VLOOKUP in Excel.

Here is an example:

value <- c(5,4,3,2,1)
min <- c(381, 201, 97, 34, 0)
max <- c(NA, 380, 200, 96, 33)
tariffs <-data.frame(value=value, min=min, max=max)

 > tariffs
  value min max
1     5 381  NA
2     4 201 380
3     3  97 200
4     2  34  96
5     1   0  33

That is tariffs within the range (0,33) will receive a value of 1, within (34,96) the value of 2 and so on.

This would be the output for give tariffs:

tariff2019 <- data.frame(t19=c(779, 536, 368, 224, 96))
tariff2019$value <- c(5,5,4,4,2)

tariff2019
  t19 value
1 779     5
2 536     5
3 368     4
4 224     4
5  96     2

How would I do this in R?

Stata_user
  • 510
  • 3
  • 12
  • 1
    Try the cut function, something in the lines of as.numeric(cut(tariff2019$value, breaks=c(0, 33.5, 96.5, 200.5,380, Inf), label=(2,4,4,5,5))) – David. Jul 10 '21 at 05:38

0 Answers0