Skip to contents

With a defined range of values, the function systematically examines each provided number to determine if it falls within the specified range. It may also provide the values with the range that are closest to a desired number.

Usage

in.range(
  value,
  range.min,
  range.max,
  range.vec = NULL,
  closest = FALSE,
  rm.na = FALSE
)

Arguments

value

NUMERIC. the vector of numbers to check

range.min

NUMERIC. OPTIONAL. the minimum value of the range

range.max

NUMERIC. OPTIONAL. the maximum value of the range

range.vec

NUMERIC. OPTIONAL. a vector of numbers to use for the range

closest

BOOLEAN. OPTIONAL. return closest value

rm.na

BOOLEAN. OPTIONAL. remove NA values from input

Value

boolean to indicate if the value or set of values are within the range

Details

The described function serves the purpose of checking whether a given number or set of numbers falls within a specified range. It operates by taking a range of values as input and then systematically evaluates each provided number to determine if it lies within the defined range. This function proves particularly useful for scenarios where there is a need to assess numeric values against predefined boundaries, ensuring they meet specific criteria or constraints. In the same manner, this function allows the user to also retrieve values within the range that are closest to each provided number.

Note

The argument range.vec is utilized when users opt not to employ the range.min or range.max arguments. If range.vec is specified, range.min and range.max are disregarded. It's important to note that the use of range.vec is optional.

Examples

# Task 1: Check if a number is within specified range
in.range(5, range.min = 3, range.max = 10) # TRUE
#> [1] TRUE
in.range(25, range.min = 12, range.max = 20) # FALSE
#> [1] FALSE

# Task 2: Check if a set of values are within a specified range
in.range(1:5, range.min = 2, range.max = 7) #
#> [1] FALSE  TRUE  TRUE  TRUE  TRUE
in.range(50:60, range.min = 16, range.max = 27) #
#>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE


# Task 3: Check if a number is within the range of a set of numbers
in.range(5, range.vec = 1:10) # TRUE
#> [1] TRUE
in.range(345, range.vec = c(1001,1002,1003,1004,1005,
1006,1007,1008,1009,1010,1011,1012,1013,1014)) # FALSE
#> [1] FALSE

# Task 4: Check if a set of values are within the range of a set of numbers
in.range(1:5, range.vec = 4:19) #
#> [1] FALSE FALSE FALSE  TRUE  TRUE
in.range(50:60, range.vec = c(55,33,22,56,75,213,120)) #
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

# Task 5: remove NAs prior to processing
in.range(c(1,3,NA,3,4,NA,8), range.min = 4, range.max = 6, rm.na = FALSE) # do not remove NA
#> [1] FALSE FALSE    NA FALSE  TRUE    NA FALSE
in.range(c(1,3,NA,3,4,NA,8), range.min = 4, range.max = 6, rm.na = TRUE) # remove NA
#> [1] FALSE FALSE FALSE  TRUE FALSE
#in.range(c(NA), range.min = 4, range.max = 6, rm.na = TRUE) #This will return error

# Task 6: return the closest number to the value
in.range(5:23, range.vec = 7:19, closest = TRUE)
#>  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 
#>  7  7  7  8  9 10 11 12 13 14 15 16 17 18 19 19 19 19 19 
in.range(-5:10, range.vec = -2:19, closest = TRUE)
#> -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8  9 10 
#> -2 -2 -2 -2 -1  0  1  2  3  4  5  6  7  8  9 10 
in.range(c(1:5,NA,6:9), range.vec = 4:19, closest = TRUE)
#>    1    2    3    4    5 <NA>    6    7    8    9 
#>    4    4    4    4    5   NA    6    7    8    9 
in.range(c(1:5,NA,6:9), range.vec = 4:19, closest = TRUE, rm.na = TRUE)
#> 1 2 3 4 5 6 7 8 9 
#> 4 4 4 4 5 6 7 8 9