R/06-wrp_aggregate.R

Defines functions wrp_aggregate

#' Get aggregated data for a question in the World Risk Poll
#'
#' Allows you to access aggregated data for a World Risk Poll question.
#'
#' @param df data frame, a demographic category by which to aggregate
#'
#' @importFrom dplyr mutate group_by_at ungroup summarise select
#' @importFrom sjlabelled label_to_colnames
#' @importFrom rlang .data
#'
#' @return data frame with aggregated World Risk Poll question data
#' @noRd
wrp_aggregate <- function(df) {
  df <- df %>%
    group_by_at(c(1, 2, 3, 5, 6)) %>%
    summarise(weightedCount = sum(.data$wgt)) %>%
    ungroup() %>%
    group_by_at(c(1, 2, 3)) %>%
    mutate(percentage = .data$weightedCount /
      sum(.data$weightedCount) * 100) %>%
    ungroup() %>%
    label_to_colnames() %>%
    select(-.data$weightedCount)
  return(df)
}

Try the worldriskpollr package in your browser

Any scripts or data that you put into this service are public.

worldriskpollr documentation built on April 3, 2023, 5:41 p.m.