Skip to content

Commit 36539cc

Browse files
authored
Refresh na_if() and coalesce() docs (#7742)
* Refresh `na_if()` docs * Refresh `coalesce()` docs * Also mention `replace_when()` After reading #5711 and #6511 * Show how `na_if(x, <multiple>)` fails
1 parent 0d6054f commit 36539cc

File tree

4 files changed

+139
-51
lines changed

4 files changed

+139
-51
lines changed

R/coalesce.R

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,48 @@
1919
#' @return A vector with the same type and size as the common type and common
2020
#' size of the vectors in `...`.
2121
#'
22-
#' @seealso [na_if()] to replace specified values with an `NA`.
23-
#' [tidyr::replace_na()] to replace `NA` with a value.
22+
#' @seealso
23+
#'
24+
#' - [na_if()] to replace a specified value with `NA`.
25+
#'
26+
#' - [replace_values()] for making arbitrary replacements by value.
27+
#'
28+
#' - [replace_when()] for making arbitrary replacements using logical
29+
#' conditions.
2430
#'
2531
#' @export
2632
#' @examples
27-
#' # Use a single value to replace all missing values
33+
#' # Replace missing values with a single value
2834
#' x <- sample(c(1:5, NA, NA, NA))
2935
#' coalesce(x, 0L)
3036
#'
31-
#' # The equivalent to a missing value in a list is `NULL`
32-
#' coalesce(list(1, 2, NULL), list(NA))
37+
#' # Or replace missing values with the corresponding non-missing value in
38+
#' # another vector
39+
#' x <- c(1, 2, NA, NA, 5, NA)
40+
#' y <- c(NA, NA, 3, 4, 5, NA)
41+
#' coalesce(x, y)
42+
#'
43+
#' # For cases like these where your replacement is a single value or a single
44+
#' # vector, `replace_values()` works just as well
45+
#' replace_values(x, NA ~ 0)
46+
#' coalesce(x, 0)
3347
#'
34-
#' # Or generate a complete vector from partially missing pieces
35-
#' y <- c(1, 2, NA, NA, 5)
36-
#' z <- c(NA, NA, 3, 4, 5)
37-
#' coalesce(y, z)
48+
#' replace_values(x, NA ~ y)
49+
#' coalesce(x, y)
50+
#'
51+
#' # `coalesce()` really shines when you have >2 vectors to coalesce with
52+
#' z <- c(NA, 2, 3, 4, 5, 6)
53+
#' coalesce(x, y, z)
54+
#'
55+
#' # If you're looking to replace values with `NA`, rather than replacing `NA`
56+
#' # with a value, then use `replace_values()`
57+
#' x <- c(0, -1, 5, -99, 8)
58+
#' replace_values(x, c(-1, -99) ~ NA)
59+
#'
60+
#' # The equivalent to a missing value in a list is `NULL`
61+
#' coalesce(list(1, 2, NULL, NA), list(0))
3862
#'
39-
#' # Supply lists by splicing them into dots:
63+
#' # Supply lists of vectors by splicing them into dots
4064
#' vecs <- list(
4165
#' c(1, 2, NA, NA, 5),
4266
#' c(NA, NA, 3, 4, 5)

R/na-if.R

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,51 @@
1515
#' but most of the time this will be a single value.
1616
#' @return A modified version of `x` that replaces any values that
1717
#' are equal to `y` with `NA`.
18-
#' @seealso [coalesce()] to replace missing values with a specified
19-
#' value.
2018
#'
21-
#' [tidyr::replace_na()] to replace `NA` with a value.
19+
#' @seealso
20+
#'
21+
#' - [coalesce()] to replace `NA`s with the first non-missing value.
22+
#'
23+
#' - [replace_values()] for making arbitrary replacements by value.
24+
#'
25+
#' - [replace_when()] for making arbitrary replacements using logical
26+
#' conditions.
27+
#'
2228
#' @export
2329
#' @examples
24-
#' na_if(1:5, 5:1)
30+
#' # `na_if()` is useful for replacing a single problematic value with `NA`
31+
#' na_if(c(-99, 1, 4, 3, -99, 5), -99)
32+
#' na_if(c("abc", "def", "", "ghi"), "")
33+
#'
34+
#' # You can use it to standardize `NaN`s to `NA`
35+
#' na_if(c(1, NaN, NA, 2, NaN), NaN)
36+
#'
37+
#' # Because `na_if()` is an R translation of SQL's `NULLIF` command,
38+
#' # it compares `x` and `y` element by element. Where `x` and `y` are
39+
#' # equal, the value in `x` is replaced with an `NA`.
40+
#' na_if(
41+
#' x = c(1, 2, 5, 5, 6),
42+
#' y = c(0, 2, 3, 5, 4)
43+
#' )
44+
#'
45+
#' # If you have multiple problematic values that you'd like to replace with
46+
#' # `NA`, then `replace_values()` is a better choice than `na_if()`
47+
#' x <- c(-99, 1, 4, 0, -99, 5, -1, 0, 5)
48+
#' replace_values(x, c(0, -1, -99) ~ NA)
2549
#'
26-
#' x <- c(1, -1, 0, 10)
27-
#' 100 / x
28-
#' 100 / na_if(x, 0)
50+
#' # You'd have to nest `na_if()`s to achieve this
51+
#' try(na_if(x, c(0, -1, -99)))
52+
#' na_if(na_if(na_if(x, 0), -1), -99)
2953
#'
30-
#' y <- c("abc", "def", "", "ghi")
31-
#' na_if(y, "")
54+
#' # If you'd like to replace values that match a logical condition with `NA`,
55+
#' # use `replace_when()`
56+
#' replace_when(x, x < 0 ~ NA)
3257
#'
33-
#' # `na_if()` allows you to replace `NaN` with `NA`,
34-
#' # even though `NaN == NaN` returns `NA`
35-
#' z <- c(1, NaN, NA, 2, NaN)
36-
#' na_if(z, NaN)
58+
#' # If you'd like to replace `NA` with some other value, use `replace_values()`
59+
#' x <- c(NA, 5, 2, NA, 0, 3)
60+
#' replace_values(x, NA ~ 0)
3761
#'
38-
#' # `na_if()` is particularly useful inside `mutate()`,
39-
#' # and is meant for use with vectors rather than entire data frames
62+
#' # `na_if()` is particularly useful inside `mutate()`
4063
#' starwars |>
4164
#' select(name, eye_color) |>
4265
#' mutate(eye_color = na_if(eye_color, "unknown"))

man/coalesce.Rd

Lines changed: 32 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/na_if.Rd

Lines changed: 35 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)