Learn R Programming

guideR (version 0.8.1)

median_iqr: Compute median, quartiles and interquartile range by sub-groups

Description

median_iqr() lets you quickly compute median, quartiles and interquartile range by sub-groups. Use .outliers = TRUE to also return whiskers and outliers (see ggplot2::stat_boxplot()).

Usage

median_iqr(data, ...)

# S3 method for data.frame median_iqr( data, ..., .by = NULL, .drop = FALSE, .drop_na_by = FALSE, .outliers = FALSE )

# S3 method for survey.design median_iqr( data, ..., .by = NULL, .drop = FALSE, .drop_na_by = FALSE, .outliers = FALSE )

# S3 method for default median_iqr(data, ..., .drop = FALSE, .outliers = FALSE)

Value

A tibble. Column "n" reports the number of valid observations and "missing" the number of missing (NA) observations, unweighted for survey objects.

A tibble with one row per group.

Arguments

data

A vector, a data frame, data frame extension (e.g. a tibble), or a survey design object.

...

<data-masking> Variable(s) for which to compute median, quartiles and interquartile range.

.by

<tidy-select> Optional additional variables to group by (in addition to those eventually previously declared using dplyr::group_by()).

.drop

If TRUE, will remove empty groups from the output.

.drop_na_by

If TRUE, will remove any NA values observed in the .by variables (or variables defined with dplyr::group_by()).

.outliers

If TRUE, will estimate whiskers and outliers.

Examples

Run this code
# using a vector
iris$Petal.Length |> median_iqr()

# one variable
iris |> median_iqr(Petal.Length)
iris |> median_iqr(Petal.Length, .outliers = TRUE)
iris |> median_iqr(Petal.Length, .by = Species)
mtcars |> median_iqr(mpg, .by = c(cyl, gear))

# two variables
iris |> median_iqr(Petal.Length, Petal.Width)
iris |> median_iqr(dplyr::pick(dplyr::starts_with("Petal")), .by = Species)

# missing values
d <- iris
d$Petal.Length[1:10] <- NA
d |> median_iqr(Petal.Length)
d |> median_iqr(Petal.Length, .by = Species)

# \donttest{
## SURVEY DATA ------------------------------------------------------

ds <- srvyr::as_survey(iris)
ds |> median_iqr(Petal.Length, .by = Species, .outliers = TRUE)
# }

Run the code above in your browser using DataLab