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
  • 52,746
  • 12
  • 114
  • 209
Tugiyo
  • 31
  • 6

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
  • 147,086
  • 7
  • 49
  • 87