bind_self: Neatly bind a data frame to itself, with a column changed

Description Usage Arguments Value Examples

View source: R/bind_self.R

Description

This function changes the value of a data frame's column, then binds back to itself in order to fit into a pipeline. This preserves grouping, and if group is a factor, returns group as a factor with expanded levels.

Usage

1
bind_self(.data, group, new_value, append = FALSE, .id = NULL)

Arguments

.data

A data frame.

group

Bare column name of the column needing to be changed.

new_value

String giving the new value for column group.

append

Logical: should new values be appended to the end of data, or prepended to the beginning (the default)?

.id

Optional, inheriting from [dplyr::bind_rows()]: if a string, will create IDs for each data frame and create a column of IDs with the name given as .id.

Value

A data frame with twice as many rows as the original.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
town_populations <- data.frame(
  name = c("East Haven", "Hamden", "West Haven"),
  pop = c(29015, 61476, 54972)
)
town_populations %>%
  dplyr::mutate(measure = "total_pop") %>%
  bind_self(group = name, new_value = "Inner Ring towns")

town_populations %>%
  dplyr::group_by(name) %>%
  bind_self(group = name, new_value = "Inner Ring towns") %>%
  dplyr::summarise(pop = sum(pop))

camille-s/camiller documentation built on Jan. 22, 2022, 6:50 a.m.