Learn R Programming

guideR (version 0.8.1)

mean_sd: Compute means, standard deviations and confidence intervals by sub-groups

Description

mean_sd() lets you quickly compute mean and standard deviation by sub-groups. Use .conf.int = TRUE to also return confidence intervals of the mean.

Usage

mean_sd(data, ...)

# S3 method for data.frame mean_sd( data, ..., .by = NULL, .drop = FALSE, .drop_na_by = FALSE, .conf.int = FALSE, .conf.level = 0.95, .options = NULL )

# S3 method for survey.design mean_sd( data, ..., .by = NULL, .drop = FALSE, .drop_na_by = FALSE, .conf.int = FALSE, .conf.level = 0.95, .options = NULL )

# S3 method for default mean_sd( data, ..., .drop = FALSE, .conf.int = FALSE, .conf.level = 0.95, .options = NULL )

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 mean and standard deviation.

.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()).

.conf.int

If TRUE, will estimate confidence intervals.

.conf.level

Confidence level for the returned confidence intervals.

.options

Additional arguments passed to stats::t.test() or srvyr::survey_mean().

Examples

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

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

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

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

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

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

Run the code above in your browser using DataLab