
Last chance! 50% off unlimited learning
Sale ends in
lifecycle::badge("stable")
Divides data into groups by a wide range of methods.
Creates a grouping factor with 1
s for group 1, 2
s for group 2, etc.
Returns a data.frame
grouped by the grouping factor for easy use in
magrittr `%>%`
pipelines.
By default*, the data points in a group are connected sequentially (e.g. c(1, 1, 2, 2, 3, 3)
)
and splitting is done from top to bottom. *Except in the "every"
method.
There are five types of grouping methods:
The "n_*"
methods split the data into a given number of groups.
They differ in how they handle excess data points.
The "greedy"
method uses a group size to split the data into groups,
greedily grabbing `n`
data points from the top.
The last group may thus differ in size (e.g. c(1, 1, 2, 2, 3)
).
The "l_*"
methods use a list of either starting points ("l_starts"
)
or group sizes ("l_sizes"
). The "l_starts"
method can also auto-detect group starts
(when a value differs from the previous value).
The "every"
method puts every `n`
th data point into the same group
(e.g. c(1, 2, 3, 1, 2, 3)
).
The step methods "staircase"
and "primes"
increase the group size by a step for each group.
Note: To create groups balanced by a categorical and/or numerical variable, see the
fold()
and partition()
functions.
group(
data,
n,
method = "n_dist",
starts_col = NULL,
force_equal = FALSE,
allow_zero = FALSE,
return_factor = FALSE,
descending = FALSE,
randomize = FALSE,
col_name = ".groups",
remove_missing_starts = FALSE
)
data.frame
grouped by existing grouping variables and the new grouping factor.
data.frame
or vector
.
When a grouped data.frame
, the function is applied group-wise.
Depends on `method`
.
Number of groups (default), group size, list of group sizes,
list of group starts, number of data points between group members,
step size or prime number to start at. See `method`
.
Passed as whole number(s) and/or percentage(s) (0
< n
< 1
)
and/or character.
Method "l_starts"
allows 'auto'
.
"greedy"
, "n_dist"
, "n_fill"
, "n_last"
,
"n_rand"
, "l_sizes"
, "l_starts"
, "every"
, "staircase"
, or
"primes"
.
Note: examples are sizes of the generated groups
based on a vector with 57
elements.
Divides up the data greedily given a specified group size
`n`
is group size.
Divides the data into a specified number of groups and
distributes excess data points across groups
`n`
is number of groups.
Divides the data into a specified number of groups and
fills up groups with excess data points from the beginning
`n`
is number of groups.
Divides the data into a specified number of groups.
It finds the most equal group sizes possible,
using all data points. Only the last group is able to differ in size
`n`
is number of groups.
Divides the data into a specified number of groups.
Excess data points are placed randomly in groups (max. 1 per group)
`n`
is number of groups.
Divides up the data by a list
of group sizes.
Excess data points are placed in an extra group at the end.
`n`
is a list
of group sizes.
Starts new groups at specified values in the `starts_col`
vector.
n
is a list
of starting positions.
Skip values by c(value, skip_to_number)
where skip_to_number
is the
nth appearance of the value in the vector after the previous group start.
The first data point is automatically a starting position.
To skip:
If passing find_starts()
).
Note that all NA
s are first replaced by a single unique value,
meaning that they will also cause group starts.
See differs_from_previous()
to set a threshold for what is considered "different".
Combines every `n`
th data point into a group.
`n`
is the number of data points between group members ("every n").
Uses step size to divide up the data.
Group size increases with 1 step for every group,
until there is no more data
`n`
is step size.
Uses prime numbers as group sizes.
Group size increases to the next prime number
until there is no more data.
`n`
is the prime number to start at.
Name of column with values to match in method "l_starts"
when `data`
is a data.frame
. Pass 'index'
to use row names. (Character)
Create equal groups by discarding excess data points. Implementation varies between methods. (Logical)
Whether `n`
can be passed as 0
.
Can be useful when programmatically finding n
. (Logical)
Only return the grouping factor. (Logical)
Change the direction of the method. (Not fully implemented) (Logical)
Randomize the grouping factor. (Logical)
Name of the added grouping factor.
Recursively remove elements from the
list of starts that are not found.
For method "l_starts"
only.
(Logical)
Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk
Other grouping functions:
all_groups_identical()
,
collapse_groups()
,
collapse_groups_by
,
fold()
,
group_factor()
,
partition()
,
splt()
Other staircase tools:
%primes%()
,
%staircase%()
,
group_factor()
Other l_starts tools:
differs_from_previous()
,
find_missing_starts()
,
find_starts()
,
group_factor()
# Attach packages
library(groupdata2)
library(dplyr)
# Create data frame
df <- data.frame(
"x" = c(1:12),
"species" = factor(rep(c("cat", "pig", "human"), 4)),
"age" = sample(c(1:100), 12)
)
# Using group()
df_grouped <- group(df, n = 5, method = "n_dist")
# Using group() in pipeline to get mean age
df_means <- df %>%
group(n = 5, method = "n_dist") %>%
dplyr::summarise(mean_age = mean(age))
# Using group() with `l_sizes`
df_grouped <- group(
data = df,
n = list(0.2, 0.3),
method = "l_sizes"
)
# Using group_factor() with `l_starts`
# `c('pig', 2)` skips to the second appearance of
# 'pig' after the first appearance of 'cat'
df_grouped <- group(
data = df,
n = list("cat", c("pig", 2), "human"),
method = "l_starts",
starts_col = "species"
)
Run the code above in your browser using DataLab