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
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
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)