Home > Software engineering >  Creating upper value and lower value based on a number in a column
Creating upper value and lower value based on a number in a column

Time:11-26

I have a column in which I have some numbers ranging from 0,5 to 0,9375.

I would like to create two additional columns that will contain

  1. lower bound of the interval
  2. higher bound of the interval

Each number in a column showed below should fall to these intervals.

The intervals are:

a) 0,5 - 0,625
b) 0,625 - 0,75
c) 0,75 - 0,8125
d) 0,8125 - 0,875
e) 0,875 - 0,9375

So for example when there is 0,63 in a row, I would like to add two columns containing:

a) 0,625  
b) 0,75

Can you please help with this? I am still learning R and it's a huge challenge for me.

Thank you.

Column

CodePudding user response:

Use findInterval to get an index into the intervals limits vector, then index this vector.

# test data
set.seed(2022)
df1 <- data.frame(IDFAVG = c(runif(20, 0.5, 0.9375), 0.5, 0.9375))

lims <- c(0.5, 0.625, 0.75, 0.8125, 0.875, 0.9375)
i <- findInterval(df1$IDFAVG, lims, rightmost.closed = TRUE, all.inside = TRUE)

df1$lo <- lims[i]
df1$up <- lims[i   1L]
df1
#>       IDFAVG     lo     up
#> 1  0.8569902 0.8125 0.8750
#> 2  0.7831760 0.7500 0.8125
#> 3  0.5526438 0.5000 0.6250
#> 4  0.7379126 0.6250 0.7500
#> 5  0.5808194 0.5000 0.6250
#> 6  0.7781585 0.7500 0.8125
#> 7  0.5325058 0.5000 0.6250
#> 8  0.5183645 0.5000 0.6250
#> 9  0.6620137 0.6250 0.7500
#> 10 0.8312981 0.8125 0.8750
#> 11 0.5008147 0.5000 0.6250
#> 12 0.5699123 0.5000 0.6250
#> 13 0.5633245 0.5000 0.6250
#> 14 0.7272361 0.6250 0.7500
#> 15 0.7666459 0.7500 0.8125
#> 16 0.5535984 0.5000 0.6250
#> 17 0.8382828 0.8125 0.8750
#> 18 0.7250977 0.6250 0.7500
#> 19 0.7605086 0.7500 0.8125
#> 20 0.8722731 0.8125 0.8750
#> 21 0.5000000 0.5000 0.6250
#> 22 0.9375000 0.8750 0.9375

Created on 2022-11-25 with reprex v2.0.2

  • Related