Changes values within a specified range, ≥
, or ≤
specific value to a new value in a vector, data.frame, or raster.
Source: R/General_Range2NewVal.R
Range2NewVal.Rd
This function modifies values in the input object x
based on the specified
conditions. It can operate on vectors, data.frames, or RasterLayer objects.
The function allows for changing values within a specified range (Between
),
≥
a specified value (MoreThan
), or ≤
a specified value (LessThan
) to
a new value (NewVal
). An option to invert the selection is also available
for ranges.
Usage
Range2NewVal(
x,
Between = NULL,
MoreThan = NULL,
LessThan = NULL,
NewVal,
InvertSelection = FALSE
)
Arguments
- x
A vector, data.frame, or RasterLayer object whose values are to be modified.
- Between
A numeric vector of length 2 specifying the range of values to be changed or kept. If specified,
MoreThan
andLessThan
are ignored.- MoreThan, LessThan
A numeric value specifying the threshold above/below which values in
x
will be changed toNewVal
. Only applied ifBetween
is not specified.- NewVal
The new value to assign to the selected elements in
x
.- InvertSelection
A logical value indicating whether to invert the selection specified by
Between
. IfTRUE
, values outside the specified range are changed toNewVal
. Default isFALSE
.
Examples
# Vector
Range2NewVal(x = seq_len(10), Between = c(5, 8), NewVal = NA)
#> [1] 1 2 3 4 NA NA NA NA 9 10
Range2NewVal(
x = seq_len(10), Between = c(5, 8), NewVal = NA, InvertSelection = TRUE)
#> [1] NA NA NA NA 5 6 7 8 NA NA
Range2NewVal(x = seq_len(10), Between = c(5, 8), NewVal = NA, MoreThan = 4)
#> [1] 1 2 3 4 NA NA NA NA 9 10
# ---------------------------------------------
# tibble
iris %>%
tibble::as_tibble() %>%
dplyr::slice_head(n = 50) %>%
dplyr::select(-Sepal.Length, -Petal.Length, -Petal.Width) %>%
dplyr::mutate(
Sepal.Width.New = Range2NewVal(
x = Sepal.Width, Between = c(3, 3.5), NewVal = NA,
InvertSelection = FALSE),
Sepal.Width.Rev = Range2NewVal(
x = Sepal.Width, Between = c(3, 3.5), NewVal = NA,
InvertSelection = TRUE)) %>%
dplyr::arrange(-Sepal.Width) %>%
print(n = 50)
#> # A tibble: 50 × 4
#> Sepal.Width Species Sepal.Width.New Sepal.Width.Rev
#> <dbl> <fct> <dbl> <dbl>
#> 1 4.4 setosa 4.4 NA
#> 2 4.2 setosa 4.2 NA
#> 3 4.1 setosa 4.1 NA
#> 4 4 setosa 4 NA
#> 5 3.9 setosa 3.9 NA
#> 6 3.9 setosa 3.9 NA
#> 7 3.8 setosa 3.8 NA
#> 8 3.8 setosa 3.8 NA
#> 9 3.8 setosa 3.8 NA
#> 10 3.8 setosa 3.8 NA
#> 11 3.7 setosa 3.7 NA
#> 12 3.7 setosa 3.7 NA
#> 13 3.7 setosa 3.7 NA
#> 14 3.6 setosa 3.6 NA
#> 15 3.6 setosa 3.6 NA
#> 16 3.6 setosa 3.6 NA
#> 17 3.5 setosa NA 3.5
#> 18 3.5 setosa NA 3.5
#> 19 3.5 setosa NA 3.5
#> 20 3.5 setosa NA 3.5
#> 21 3.5 setosa NA 3.5
#> 22 3.5 setosa NA 3.5
#> 23 3.4 setosa NA 3.4
#> 24 3.4 setosa NA 3.4
#> 25 3.4 setosa NA 3.4
#> 26 3.4 setosa NA 3.4
#> 27 3.4 setosa NA 3.4
#> 28 3.4 setosa NA 3.4
#> 29 3.4 setosa NA 3.4
#> 30 3.4 setosa NA 3.4
#> 31 3.4 setosa NA 3.4
#> 32 3.3 setosa NA 3.3
#> 33 3.3 setosa NA 3.3
#> 34 3.2 setosa NA 3.2
#> 35 3.2 setosa NA 3.2
#> 36 3.2 setosa NA 3.2
#> 37 3.2 setosa NA 3.2
#> 38 3.2 setosa NA 3.2
#> 39 3.1 setosa NA 3.1
#> 40 3.1 setosa NA 3.1
#> 41 3.1 setosa NA 3.1
#> 42 3.1 setosa NA 3.1
#> 43 3 setosa NA 3
#> 44 3 setosa NA 3
#> 45 3 setosa NA 3
#> 46 3 setosa NA 3
#> 47 3 setosa NA 3
#> 48 3 setosa NA 3
#> 49 2.9 setosa 2.9 NA
#> 50 2.3 setosa 2.3 NA
# ---------------------------------------------
# raster
library(raster)
RRR <- raster::raster(system.file("external/test.grd", package = "raster"))
RRR2 <- Range2NewVal(x = RRR, LessThan = 500, NewVal = NA)
RRR3 <- Range2NewVal(x = RRR, MoreThan = 500, NewVal = NA)
par(mar = c(0.5, 0.5, 3, 3))
plot(
raster::stack(RRR, RRR2, RRR3), nr = 1,
main = c("Original", "<500 to NA", ">500 to NA"))
RRR2 <- Range2NewVal(
x = RRR, Between = c(1000, 1800), NewVal = 1800, InvertSelection = FALSE)
RRR3 <- Range2NewVal(
x = RRR, Between = c(1000, 1800), NewVal = 1800, InvertSelection = TRUE)
plot(
raster::stack(RRR>=1000, RRR2, RRR3), nr = 1,
main = c(">1000 ?", "<500 to NA", ">500 to NA"))