gsum {timeplyr} | R Documentation |
Grouped statistical functions.
Description
These functions are wrappers around the collapse equivalents
but always return a vector the same length and same order as x.
They all accept group IDs for grouped calculations.
Usage
gsum(x, g = NULL, na.rm = TRUE, ...)
gmean(x, g = NULL, na.rm = TRUE, ...)
gmin(x, g = NULL, na.rm = TRUE, ...)
gmax(x, g = NULL, na.rm = TRUE, ...)
gsd(x, g = NULL, na.rm = TRUE, ...)
gvar(x, g = NULL, na.rm = TRUE, ...)
gmode(x, g = NULL, na.rm = TRUE, ...)
gmedian(x, g = NULL, na.rm = TRUE, ...)
gfirst(x, g = NULL, na.rm = TRUE, ...)
glast(x, g = NULL, na.rm = TRUE, ...)
gnobs(x, g = NULL, ...)
Arguments
x |
An atomic vector. |
g |
Group IDs passed directly to |
na.rm |
Should |
... |
Additional parameters passed on to the collapse package
equivalents, |
Value
A vector the same length as x
.
Examples
library(timeplyr)
library(dplyr)
library(ggplot2)
# Dplyr
iris %>%
mutate(mean = mean(Sepal.Length), .by = Species)
# Timeplyr
iris %>%
mutate(mean = gmean(Sepal.Length, g = Species))
# One can utilise pick() to specify multiple groups
mpg %>%
mutate(mean = gmean(displ, g = pick(manufacturer, model)))
# Alternatively you can create a unique ID for each group
mpg %>%
add_group_id(manufacturer, model) %>%
mutate(mean = gmean(displ, g = group_id))
# Another example
iris %>%
add_group_id(Species, .name = "g") %>%
mutate(min = gmin(Sepal.Length, g = g),
max = gmax(Sepal.Length, g = g),
sum = gsum(Sepal.Length, g = g),
mean = gmean(Sepal.Length, g = g)) %>%
# The below is equivalent to above
mutate(min2 = min(Sepal.Length),
max2 = max(Sepal.Length),
sum2 = sum(Sepal.Length),
mean2 = mean(Sepal.Length),
.by = Species) %>%
distinct(Species,
min, min2,
max, max2,
sum, sum2,
mean, mean2)
[Package timeplyr version 0.5.0 Index]