mutate_qc: Report number of NAs created when performing dplyr mutate

mutate_qcR Documentation

Report number of NAs created when performing dplyr mutate

Description

mutate_qc and transmute_qc return identical objects as dplyr::mutate and dplyr::transmute. Like dplyr, mutate_qc adds new variables and preserves existing variables, and transmute_qc drops existing variables.

Usage

mutate_qc(.data, ..., .group_check = F)

transmute_qc(.data, ..., .group_check = F)

Arguments

.data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.

...

<data-masking> Name-value pairs. The name gives the name of the column in the output.

The value can be:

  • A vector of length 1, which will be recycled to the correct length.

  • A vector the same length as the current group (or the whole data frame if ungrouped).

  • NULL, to remove the column.

  • A data frame or tibble, to create multiple columns in the output.

.group_check

a logical value, that when TRUE, will print a table with each group variable, and columns called "var_name" and "n_missing" that together indicate, for each group, how many values are missing of newly created variables. Only variables that contain at least 1 missing value are reported. This has no effect on the returned object, and only prints information. Default is FALSE, to avoid excess printing. If data is not grouped and .group_check = T, then an error is thrown.

Details

mutate_qc and transmute_qc are used exactly the same as mutate and transmute and require all of the same arguments and return identical objects. The only difference is that the _qc versions print a message indicating the number of NA or INFinite values created in the new or edited variable(s).

Value

An object of the same class as .data. This object will be identical to that which is returned when running dplyr::mutate or dplyr::transmute functions.

Scoped variants

There are _qc versions of the scoped mutate functions. See mutate_at_qc, mutate_all_qc, or mutate_if_qc. Or transmute_at_qc, transmute_all_qc, or transmute_if_qc.

Grouping

All functions work with grouped data.

See Also

mutate

Examples

practice_data <- 
  data.frame(
  A = c(1:4, NA), 
  B = c(NA, 7:10), 
  C = 21:25,
  G = c("X", "X", "X", "Y", "Y"),
  stringsAsFactors = F
)

# Use the _qc versions just like normal dplyr mutate functions
mutate_qc(practice_data, new_var_1 = A + B, new_var_2 = A - C)

# mutate_qc will only report the number of NAs and INFs in the final copy of
# the variable, so if you mutate the same variable more thna once in the 
# call, it's only the final outcome that gets tracked
mutate_qc(practice_data, new_var = A + B, new_var = C + 1)

# Functions worked on grouped data, too
grouped_data <- dplyr::group_by(practice_data, G)
mutate_qc(grouped_data, new_var_1 = A + mean(B), mean_b = mean(B))

# Setting .group_check = T will also print a table indicating which groups
# have a missing value, on what variable, and how many values are missing.
mutate_qc(grouped_data, new_var_1 = A + mean(B), mean_b = mean(B), .group_check = T)


adamMaier/reviewr documentation built on Nov. 5, 2023, 7:21 a.m.