R/with_min_cy.R

Defines functions with_min_cy

Documented in with_min_cy

#' Select Items with Observations in a Minimum Number of Distinct Country-Years
#'
#' \code{with_min_cy} is a convenience function for filtering data to be input to \code{dcpo} by the number of country-year-items observed
#'
#' @param x a data frame of survey items and marginals generated by \code{dcpo_setup}
#' @param min_yrs the minimum number of distinct country-years needed for an item included in x to be retained
#'
#' @return a data frame
#'
#' @importFrom dplyr "%>%" filter mutate group_by ungroup
#'

#' @export
with_min_cy <- function(x, min_cys) {
    if (!is.na(min_cys)) {
        country <- cy_obs <- NULL

        x <- x %>%
            group_by(item) %>%
            mutate(cy_obs = length(unique(paste(country, year)))) %>%
            filter(cy_obs >= min_cys) %>%
            select(-cy_obs) %>%
            ungroup()
    }
    return(x)
}
fsolt/DCPOtools documentation built on June 9, 2025, 4:10 p.m.