proportion | R Documentation |
proportion()
lets you quickly count observations (like dplyr::count()
)
and compute relative proportions. Proportions are computed separately by
group (see examples).
proportion(data, ...)
## S3 method for class 'data.frame'
proportion(
data,
...,
.by = NULL,
.na.rm = FALSE,
.weight = NULL,
.scale = 100,
.sort = FALSE,
.drop = FALSE,
.drop_na_by = FALSE,
.conf.int = FALSE,
.conf.level = 0.95,
.options = list(correct = TRUE)
)
## S3 method for class 'survey.design'
proportion(
data,
...,
.by = NULL,
.na.rm = FALSE,
.scale = 100,
.sort = FALSE,
.drop_na_by = FALSE,
.conf.int = FALSE,
.conf.level = 0.95,
.options = NULL
)
## Default S3 method:
proportion(
data,
...,
.na.rm = FALSE,
.scale = 100,
.sort = FALSE,
.drop = FALSE,
.conf.int = FALSE,
.conf.level = 0.95,
.options = list(correct = TRUE)
)
data |
A vector, a data frame, data frame extension (e.g. a tibble), or a survey design object. |
... |
< |
.by |
< |
.na.rm |
Should |
.weight |
< |
.scale |
A scaling factor applied to proportion. Use |
.sort |
If |
.drop |
If |
.drop_na_by |
If |
.conf.int |
If |
.conf.level |
Confidence level for the returned confidence intervals. |
.options |
Additional arguments passed to |
A tibble.
A tibble with one row per group.
# using a vector
titanic$Class |> proportion()
# univariable table
titanic |> proportion(Class)
titanic |> proportion(Class, .sort = TRUE)
titanic |> proportion(Class, .conf.int = TRUE)
titanic |> proportion(Class, .conf.int = TRUE, .scale = 1)
# bivariable table
titanic |> proportion(Class, Survived) # proportions of the total
titanic |> proportion(Survived, .by = Class) # row proportions
titanic |> # equivalent syntax
dplyr::group_by(Class) |>
proportion(Survived)
# combining 3 variables or more
titanic |> proportion(Class, Sex, Survived)
titanic |> proportion(Sex, Survived, .by = Class)
titanic |> proportion(Survived, .by = c(Class, Sex))
# missing values
dna <- titanic
dna$Survived[c(1:20, 500:530)] <- NA
dna |> proportion(Survived)
dna |> proportion(Survived, .na.rm = TRUE)
## SURVEY DATA ------------------------------------------------------
ds <- srvyr::as_survey(titanic)
# univariable table
ds |> proportion(Class)
ds |> proportion(Class, .sort = TRUE)
ds |> proportion(Class, .conf.int = TRUE)
ds |> proportion(Class, .conf.int = TRUE, .scale = 1)
# bivariable table
ds |> proportion(Class, Survived) # proportions of the total
ds |> proportion(Survived, .by = Class) # row proportions
ds |> dplyr::group_by(Class) |> proportion(Survived)
# combining 3 variables or more
ds |> proportion(Class, Sex, Survived)
ds |> proportion(Sex, Survived, .by = Class)
ds |> proportion(Survived, .by = c(Class, Sex))
# missing values
dsna <- srvyr::as_survey(dna)
dsna |> proportion(Survived)
dsna |> proportion(Survived, .na.rm = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.