0

I have data like this df:

A    rate
10    ...
20

How to rate A[] with this rule with the simplest code in R

#if A<20  rate=1      if 20<A[]<30 rate=2

Thanks

zx8754
  • 46,390
  • 10
  • 104
  • 180
IFICMA
  • 9
  • 4

1 Answers1

0

There are several ways to do this, perhaps the simplest and most general being findInterval:

set.seed(1)

A <- sample(40, 5)

A
#> [1]  4 39  1 34 23

rate <- findInterval(A, c(-Inf, 20, 30, Inf))

rate
#> [1] 1 3 1 3 2

Created on 2022-02-17 by the reprex package (v2.0.1)

Allan Cameron
  • 91,771
  • 6
  • 28
  • 55