Completes year groups in the data frame with the expected year values, see fcds_const().

complete_year_groups(data, ..., year_gt = NULL, year_lt = NULL,
  year_group = year_group, year_group_levels = NULL, fill = list(n =
  0))

Arguments

data

A data frame

...

Ignored if data is a grouped data frame. If not grouped, additional arguments are passed to tidyr::complete(). Use these arguments specify which columns are included in the expansion and how. See tidyr::complete() for more information.

year_gt

Optional earliest year to include (inclusive)

year_lt

Optional latest year to include (inclusive)

year_group

The unquoted column containing the year_group.

year_group_levels

The expected year groups that should appear in the data set. If not supplied, the expected year groups from the latest FCDS release will be used instead.

fill

Default values for rows in columns added to the data

See also

Examples

d_year <- tidyr::crossing( sex = "Female", race = fcds_const("race"), year_group = c("1981-1985", "1986-1990", "1991-1995", "1996-2000", "2001-2005", "2006-2010", "2011-2015") ) %>% dplyr::mutate(year_group = factor(year_group)) # These two versions are equivalent. The first version completes all variables # included in the grouping and the second explicitly declares the variables # that should be completed. d_year %>% dplyr::group_by(sex, race) %>% complete_year_groups() %>% dplyr::arrange(sex, race, year_group)
#> # A tibble: 56 x 3 #> sex race year_group #> <chr> <chr> <chr> #> 1 Female Black 1981-1985 #> 2 Female Black 1982-1986 #> 3 Female Black 1986-1990 #> 4 Female Black 1987-1991 #> 5 Female Black 1991-1995 #> 6 Female Black 1992-1996 #> 7 Female Black 1996-2000 #> 8 Female Black 1997-2001 #> 9 Female Black 2001-2005 #> 10 Female Black 2002-2006 #> # … with 46 more rows
d_year %>% complete_year_groups(sex, race) %>% dplyr::arrange(sex, race, year_group)
#> # A tibble: 56 x 3 #> sex race year_group #> <chr> <chr> <chr> #> 1 Female Black 1981-1985 #> 2 Female Black 1982-1986 #> 3 Female Black 1986-1990 #> 4 Female Black 1987-1991 #> 5 Female Black 1991-1995 #> 6 Female Black 1992-1996 #> 7 Female Black 1996-2000 #> 8 Female Black 1997-2001 #> 9 Female Black 2001-2005 #> 10 Female Black 2002-2006 #> # … with 46 more rows
# If you have previously filtered the data to include a subset of years, you # will likely want to exclude those years from the group completion. # You can use `year_gt` and `year_lt` to exclude years beyond the boundaries. d_year %>% dplyr::filter(!year_group %in% c("1981-1985", "1986-1990")) %>% complete_year_groups(year_gt = 1990) %>% dplyr::arrange(sex, race, year_group)
#> # A tibble: 25 x 3 #> sex race year_group #> <chr> <chr> <chr> #> 1 Female Black 1991-1995 #> 2 Female Black 1996-2000 #> 3 Female Black 2001-2005 #> 4 Female Black 2006-2010 #> 5 Female Black 2011-2015 #> 6 Female Other 1991-1995 #> 7 Female Other 1996-2000 #> 8 Female Other 2001-2005 #> 9 Female Other 2006-2010 #> 10 Female Other 2011-2015 #> # … with 15 more rows