-
Notifications
You must be signed in to change notification settings - Fork 9
/
aw_weight.R
111 lines (90 loc) · 3.33 KB
/
aw_weight.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#' Calculate Areal Weight
#'
#' @description \code{aw_weight} creates an area weight field by dividing the area
#' field by the total area field. This is the third step in the interpolation
#' process after \link{aw_weight}.
#'
#' @usage aw_weight(.data, areaVar, totalVar, areaWeight)
#'
#' @param .data A \code{sf} object that has been intersected using \link{aw_intersect}
#' @param areaVar The name of the variable measuring a feature's area
#' @param totalVar The name of the variable containing total area field by \code{source} id
#' @param areaWeight The name of a new area weight field to be calculated
#'
#' @return A \code{sf} object with the intersected data and new area weight field.
#'
#' @examples
#' library(dplyr)
#'
#' race <- select(ar_stl_race, GEOID, TOTAL_E)
#' wards <- select(ar_stl_wards, WARD)
#'
#' wards %>%
#' aw_intersect(source = race, areaVar = "area") %>%
#' aw_total(source = race, id = GEOID, areaVar = "area", totalVar = "totalArea",
#' weight = "sum", type = "extensive") -> intersect
#'
#' aw_weight(intersect, areaVar = "area", totalVar = "totalArea", areaWeight = "areaWeight")
#'
#' @importFrom dplyr mutate
#' @importFrom glue glue
#' @importFrom rlang :=
#' @importFrom rlang enquo
#' @importFrom rlang quo
#' @importFrom rlang quo_name
#' @importFrom rlang sym
#'
#' @export
aw_weight <- function(.data, areaVar, totalVar, areaWeight){
# save parameters to list
paramList <- as.list(match.call())
# check for missing parameters
if (missing(.data)) {
stop("A sf object containing intersected data must be specified for the '.data' argument.")
}
if (missing(areaVar)) {
stop("A variable name must be specified for the 'areaVar' argument.")
}
if (missing(totalVar)) {
stop("A variable name must be specified for the 'totalVar' argument.")
}
if (missing(areaWeight)) {
stop("A variable name must be specified for the 'areaWeight' argument.")
}
# nse
if (!is.character(paramList$areaVar)) {
areaVarQ <- rlang::enquo(areaVar)
} else if (is.character(paramList$areaVar)) {
areaVarQ <- rlang::quo(!! rlang::sym(areaVar))
}
areaVarQN <- rlang::quo_name(rlang::enquo(areaVar))
if (!is.character(paramList$totalVar)) {
totalVarQ <- rlang::enquo(totalVar)
} else if (is.character(paramList$totalVar)) {
totalVarQ <- rlang::quo(!! rlang::sym(totalVar))
}
totalVarQN <- rlang::quo_name(rlang::enquo(totalVar))
if (!is.character(paramList$areaWeight)) {
areaWeightQ <- rlang::enquo(areaWeight)
} else if (is.character(paramList$areaWeight)) {
areaWeightQ <- rlang::quo(!! rlang::sym(areaWeight))
}
areaWeightQN <- rlang::quo_name(rlang::enquo(areaWeight))
# check variables
if (!!areaVarQN != "...area"){
if(!!areaVarQN %in% colnames(.data) == FALSE) {
stop(glue::glue("Variable '{var}', given for the area, cannot be found in the given intersected object.",
var = areaVarQN))
}
}
if (!!totalVarQN != "...totalArea"){
if(!!totalVarQN %in% colnames(.data) == FALSE) {
stop(glue::glue("Variable '{var}', given for the total area, cannot be found in the given intersected object.",
var = totalVarQN))
}
}
# calculate area weight of intersection slivers
out <- dplyr::mutate(.data, !!areaWeightQN := !!areaVarQ / !!totalVarQ)
# return output
return(out)
}