total_col_add: Create tidycensus total column

View source: R/total_col_add.R

total_col_addR Documentation

Create tidycensus total column

Description

A function to pull one or multiple total values in a tidycensus dataset for a group or groups from a row and make it into a column. This is best used after separate_label; the group total will be NA after the label is separated, so you enter the name of the group column as the total_col value e.g., ("pop_total" = race_ethnicity) to create a column for the population total in a dataset where race_ethnicity has the racial/ethnic group for each row.

Usage

total_col_add(df, total_cols, join_col = "name", est_col = "estimate")

Arguments

df

a tidycensus dataframe with a total represented as one row

total_cols

a list, formatted as list(name of new column = name column to pull total for). If you've separated the label column, the name of column to pull for will be NA.

join_col

a column or multiple columns to join the totals to. Often name, unless a race-disaggregated dataset without an aggregate total across races (e.g., race by age). If multiple totals are being calculated (i.e., length of total_cols > 1), then the columns are joined in sequence (e.g., pulling totals for race, then race by age, then race by age by gender). In these cases, the join_col will usually be a vector consisting of the unique identifier column (usually "name") followed by the values (not names) of the total_cols list except for the last value. If the there is one total column and multiple join columns, the columns are joined all at once (e.g., to generate unique totals for a place and race). In situations involving a sequence where a unique observation represents a combination of two variables, it's best to merge the two variables in advance to one column.

est_col

the name of the column containing values for each group; unless renamed or transformed, estimate

Value

a tidycensus dataset with totals for different groups as columns


dpowerstp/acsprocess documentation built on Sept. 4, 2022, 10:18 a.m.