/
DDextract_PopCounts.R
75 lines (66 loc) · 3.58 KB
/
DDextract_PopCounts.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#' DDextract_PopCounts
#'
#' Extract population counts by age and sex for a given country and time period can choose process = "census", "estimate", or "register"
#'
#' @param locid The Location Id for each country. Run `DemoTools::get_locations()` to get information about available locations. The ids are indicated in the `PK_LocID` variable.
#' @param process The data collection process i.e. Census (census), Estimate (estimate) or Register (register).
#' @param start_year The minimum year for the data available for each country
#' @param end_year The maximum year for the data available for each country
#' @param DataSourceShortName NULL.
#' @param DataSourceYear NULL.
#' @return A dataset showing population counts for each location, process, year, sex and age label.
#' @export
#'
#' @examples
#' \dontrun{
#' dd_extract <- DDextract_PopCounts(404, #Kenya
#' process = c("census","estimate","register"),
#' 1950,
#' 2050,
#' DataSourceShortName = NULL,
#' DataSourceYear = NULL)
#'}
DDextract_PopCounts <- function(locid,
process = c("census","estimate","register"),
start_year,
end_year,
DataSourceShortName = NULL,
DataSourceYear = NULL) {
## Indicate the data process id. dpi == 2 if process is census, 6 if process is estimate and 9 if process is register
dpi <- NA
dpi <- ifelse(process == "census", 2, dpi)
dpi <- ifelse(process == "estimate", 6, dpi)
dpi <- ifelse(process == "register", 9, dpi)
# Abridged or five year age groups
tryCatch({
pop_abridged <- get_recorddata(locIds = locid,
dataProcessIds = dpi,
indicatorIds = 58,# Population by age and sex (abridged)
startYear = start_year,
endYear = end_year,
locAreaTypeIds = 2, # whole area (as opposed to urban/rural or some other sub-national unit)
subGroupIds = 2, # Total or all groups (as opposed to some population subgroup)
dataSourceShortNames = DataSourceShortName,
dataSourceYears = DataSourceYear)
}, error=function(e){check_locid(locid)})
if (exists('pop_abridged')) {
pop_abridged <- pop_abridged
} else { pop_abridged <- NULL }
# By single year of age
tryCatch({
pop_single <- get_recorddata(locIds = locid,
dataProcessIds = dpi,
indicatorIds = 60,# Population by age and sex (Complete by single years of age)
startYear = start_year,
endYear = end_year,
locAreaTypeIds = 2, # whole area (as opposed to urban/rural or some other sub-national unit)
subGroupIds = 2, # Total or all groups (as opposed to some population subgroup)
dataSourceShortNames = DataSourceShortName,
dataSourceYears = DataSourceYear)
}, error=function(e){check_locid(locid)})
if (exists('pop_single')) {
pop_single <- pop_single
} else { pop_single <- NULL }
outdata <- rbind(pop_abridged, pop_single)
return(outdata)
}