View source: R/rank_in_group.R
rank_in_group | R Documentation |
Rank within a group of specified variables in a data frame. I can't remember where I saw this function originally but it's a simple clean way to do the intended task.
rank_in_group
is intended to be used in a pipe where dplyr::group_by()
and dplyr::arrange()
were applied already. See the example below for this
use.
rank_in_group2
takes a data frame, grouping variable, and arranging
variable as arguments and returns a data frame with a column, rank
added.
Similar to rank_in_group
but works on it's own outside a pipe.
rank_in_group(data)
rank_in_group2(data, group_var, arrange_var)
data |
A data frame |
group_var |
A variable to group by |
arrange_var |
A variable to arrange (descending) by |
A data frame with an added column rank
library(dplyr)
res <- iris %>%
dplyr::group_by(Species) %>%
dplyr::arrange(dplyr::desc(Sepal.Length)) %>%
rank_in_group(.)
# display first few results
res %>%
dplyr::filter(rank <= 3)
# There is also a way to do this just using dplyr:
by_species <- iris %>%
dplyr::arrange(Species, Sepal.Length) %>%
dplyr::group_by(Species) %>%
dplyr::mutate(rank = rank(Sepal.Length, ties.method = "first"))
by_species %>%
dplyr::filter(rank <= 3)
library(dplyr)
res2 <- rank_in_group2(data = iris,
group_var = Species,
arrange_var = Sepal.Length)
# display first few results
res2 %>%
dplyr::filter(rank <= 3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.