Is there a way to replace values in a data.frame
column that are above or below set threshold values with the max/min threshold values determined by the user in a single step?
The data.table::between()
function returns TRUE
or FALSE
but no indication of whether it's above or below...
See below for MWE. I can get the result in 2 steps but was wondering if there was already a built in function for replacing values above/below the max/min values with the max/min values.
Thanks.
library(data.table)
library(magrittr)
a <- data.table(colA = LETTERS[seq(1,10)],
colB = 1:10)
the_max <- 7
the_min <- 3
# creates TRUE/FALSE column...
a[, colC := between(colB, the_min, the_max)]
a
#> colA colB colC
#> 1: A 1 FALSE
#> 2: B 2 FALSE
#> 3: C 3 TRUE
#> 4: D 4 TRUE
#> 5: E 5 TRUE
#> 6: F 6 TRUE
#> 7: G 7 TRUE
#> 8: H 8 FALSE
#> 9: I 9 FALSE
#> 10: J 10 FALSE
# gets the result...
a[, colD := colB] %>%
.[colD < the_min, colD := the_min] %>%
.[colD > the_max, colD := the_max]
a
#> colA colB colC colD
#> 1: A 1 FALSE 3
#> 2: B 2 FALSE 3
#> 3: C 3 TRUE 3
#> 4: D 4 TRUE 4
#> 5: E 5 TRUE 5
#> 6: F 6 TRUE 6
#> 7: G 7 TRUE 7
#> 8: H 8 FALSE 7
#> 9: I 9 FALSE 7
#> 10: J 10 FALSE 7
Created on 2019-08-12 by the reprex package (v0.2.1)