Skip to content

Commit

Permalink
Remove areas from ipc_get_population()
Browse files Browse the repository at this point in the history
  • Loading branch information
caldwellst committed Jun 5, 2023
1 parent 040da19 commit b31c546
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 91 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: Ripc
Title: Download and Process IPC Data
Version: 0.1.3
Version: 0.1.4
Authors@R:
person(
given = "Seth",
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Ripc 0.1.4

* Fixed `ipc_get_populations()` by removing calculations for the `areas` data
frame which was removed from the API

# Ripc 0.1.3

* `ipc_get_icons()` and `ipc_get_populations()` changed so all date columns
Expand Down
89 changes: 9 additions & 80 deletions R/ipc_get_population.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
#' pass in `id`. You cannot pass in both sets of parameters.
#'
#' Unlike the other `ipc_get_..()` family of functions, this returns a list of
#' datasets, corresponding to `country` data, `groups` data, and
#' `areas`/`points`data. The benefit of `ipc_get_population()` is that the
#' datasets, corresponding to `country` and `groups` data. The benefit of
#' `ipc_get_population()` is that the
#' returned data for each level of analysis contains all periods of analysis.
#' Areas and points data is the lowest level of IPC analysis where population
#' estimates for each phase are provided and a general area-level classification
#' is made. Groups data, where available, are geographies within a country that
#' Groups data, where available, are geographies within a country that
#' comprise multiple areas and/or points. There is no phase classification at
#' the group level, but populations in each phase are provided. The same applies
#' to country-level data.
Expand All @@ -22,7 +20,6 @@
#'
#' * Country data: [ipc_get_country()]
#' * Groups data: Not available through other functions
#' * Areas and points data: [ipc_get_areas()]
#'
#' See the respective function documentation for more details on what each
#' dataset comprises or the [IPC website](https://www.ipcinfo.org) and
Expand All @@ -35,17 +32,16 @@
#'
#' @examples
#' \dontrun{
#' # get all areas from the public API
#' # get all populations from the public API
#' ipc_get_population()
#'
#' # get areas for specific analysis ID and period from developer API
#' # get populations for specific analysis ID and period from developer API
#' ipc_get_population(id = 12856213)
#' }
#'
#' @return A list of 3 data frames:
#' @return A list of 2 data frames:
#' * Country data frame
#' * Groups data frame
#' * Areas data frame
#'
#' @export
ipc_get_population <- function(
Expand Down Expand Up @@ -107,7 +103,7 @@ clean_population_df <- function(df) {
create_date_columns() %>%
dplyr::select(
-dplyr::any_of(
c("groups", "areas")
"groups"
)
) %>%
dplyr::distinct() %>%
Expand All @@ -120,7 +116,7 @@ clean_population_df <- function(df) {
!sapply(.data$groups, is.null)
) %>%
dplyr::select(
-c(dplyr::any_of("areas"), dplyr::matches("^phase|^estimated"))
-dplyr::matches("^phase|^estimated")
) %>%
dplyr::mutate(
"groups" := purrr::map(.data$groups, dplyr::as_tibble)
Expand All @@ -145,76 +141,9 @@ clean_population_df <- function(df) {
groups_df <- NULL
}


# extract areas data frame
# have to do in two stages, extracting areas that don't have groups
# then extracting again from areas under groups
if ("areas" %in% names(renamed_df)) {
renamed_areas_df1 <- renamed_df %>%
dplyr::filter(
!sapply(.data$areas, is.null)
) %>%
dplyr::select(
-c(dplyr::any_of("groups"), dplyr::matches("^phase|^estimated"))
) %>%
dplyr::mutate(
"areas" := purrr::map(.data$areas, dplyr::as_tibble)
) %>%
tidyr::unnest(
cols = "areas"
) %>%
rename_population_df()
} else {
renamed_areas_df1 <- NULL
}


# now extract areas that are under groups
if ("groups" %in% names(renamed_df)) {
renamed_areas_df2 <- renamed_groups_df %>%
dplyr::filter(
!sapply(.data$areas, is.null)
) %>%
dplyr::mutate(
"areas" := purrr::map(.data$areas, dplyr::as_tibble)
) %>%
dplyr::select(
-c(dplyr::matches("^phase|^estimated"))
) %>%
tidyr::unnest(
cols = "areas"
) %>%
rename_population_df()
} else {
renamed_areas_df2 <- NULL
}


# now combine into a single areas dataset
# TODO: remove `-dplyr::starts_with("group")` once
# the IPC team fixes the API, currently it duplicates
# all areas in a country for each group
areas_df <- dplyr::bind_rows(renamed_areas_df1, renamed_areas_df2) %>%
pivot_population_df() %>%
dplyr::filter(
.data$period_dates != ""
) %>%
create_date_columns() %>%
dplyr::distinct(
dplyr::across(
-dplyr::starts_with("group") # TODO: remove once API is fixed
)
) %>%
dplyr::rename(
"area_id" := "id",
"area_name" := "name"
) %>%
arrange_population_df()

list(
"country" = country_df,
"groups" = groups_df,
"areas" = areas_df
"groups" = groups_df
)
}

Expand Down
16 changes: 6 additions & 10 deletions man/ipc_get_population.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b31c546

Please sign in to comment.