rank_in_group: Create a rank variable by group in a data frame

View source: R/rank_in_group.R

rank_in_groupR Documentation

Create a rank variable by group in a data frame

Description

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.

Usage

rank_in_group(data)

rank_in_group2(data, group_var, arrange_var)

Arguments

data

A data frame

group_var

A variable to group by

arrange_var

A variable to arrange (descending) by

Value

A data frame with an added column rank

Examples

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)

emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.