This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
month_tools.R
174 lines (152 loc) · 5.21 KB
/
month_tools.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#' Extract Month from Year List Object
#'
#' @description This function extracts a given month from a list containing 12
#' tibbles - one per month - for additional data cleaning prior to collapsing
#' the list. Since months are ordered alphabetically in the year list objects,
#' this function makes the process of extracting a particular month more
#' intuitive.
#'
#' @usage cs_extract_month(.data, month)
#'
#' @param .data A list containing monthly crime data
#'
#' @param month A string name or abbreviation of a month, or its numeric value.
#' Acceptable inputs include, for example, "January", "january", "Jan",
#' "jan", and 1.
#'
#' @return A tibble containing a single month worth of crime data.
#'
#' @seealso \code{\link{cs_replace_month}}
#'
#' @examples
#' # load example year-list object
#' load(system.file("testdata", "yearList17.rda", package = "compstatr", mustWork = TRUE))
#'
#' # extract May
#' may17 <- cs_extract_month(yearList17, month = 5)
#' may17 <- cs_extract_month(yearList17, month = "May")
#'
#' @importFrom rlang quo
#' @importFrom rlang enquo
#' @importFrom rlang quo_name
#'
#' @export
cs_extract_month <- function(.data, month){
# check for missing parameters
if (missing(.data)) {
stop('A existing year-list object must be specified for .data.')
}
if (missing(month)) {
stop('The month to be extracted must be specified.')
}
# quote input variables
month <- rlang::quo_name(rlang::enquo(month))
# convert to lower case
month <- tolower(month)
# identify input month
if (month == "january" | month == "jan" | month == 1){
val <- "January"
} else if (month == "february" | month == "feb" | month == 2){
val <- "February"
} else if (month == "march" | month == "mar" | month == 3){
val <- "March"
} else if (month == "april" | month == "apr" | month == 4){
val <- "April"
} else if (month == "may" | month == 5){
val <- "May"
} else if (month == "june" | month == "jun" | month == 6){
val <- "June"
} else if (month == "july" | month == "jul" | month == 7){
val <- "July"
} else if (month == "august" | month == "aug" | month == 8){
val <- "August"
} else if (month == "september" | month == "sept" | month == "sep" | month == 9){
val <- "September"
} else if (month == "october" | month == "oct" | month == 10){
val <- "October"
} else if (month == "november" | month == "nov" | month == 11){
val <- "November"
} else if (month == "december" | month == "dec" | month == 12){
val <- "December"
} else {
stop("The given argument for month does not match an acceptible input.")
}
# create output
monthData <- .data[[val]]
# return output
return(monthData)
}
#' Extract Month from Year-list Object
#'
#' @description This function replaces a single month worth of crime data that
#' has previously been extracted from a year-list object.
#'
#' @usage cs_replace_month(.data, month, monthData)
#'
#' @param .data A year list object
#' @param month A string name or abbreviation of a month, or its numeric value.
#' Acceptable inputs include, for example, "January", "january", "Jan",
#' "jan", and 1.
#' @param monthData A tibble containing a single month worth of crime data.
#'
#' @return An updated year-list object.
#'
#' @seealso \code{\link{cs_extract_month}}
#'
#' @examples
#' # load example year-list object
#' load(system.file("testdata", "yearList17.rda", package = "compstatr", mustWork = TRUE))
#'
#' # extract May
#' may17 <- cs_extract_month(yearList17, month = 5)
#'
#' # replace
#' yearList17 <- cs_replace_month(yearList17, month = 5, monthData = may17)
#' yearList17 <- cs_replace_month(yearList17, month = "May", monthData = may17)
#'
#' @export
cs_replace_month <- function(.data, month, monthData){
# check for missing parameters
if (missing(.data)) {
stop('A existing year-list object must be specified for .data.')
}
if (missing(month)) {
stop('The month to be replaced must be specified.')
}
if (missing(monthData)) {
stop('The month object to be replaced must be specified.')
}
# convert to lower case
month <- tolower(month)
if (month == "january" | month == "jan" | month == 1){
val <- "January"
} else if (month == "february" | month == "feb" | month == 2){
val <- "February"
} else if (month == "march" | month == "mar" | month == 3){
val <- "March"
} else if (month == "april" | month == "apr" | month == 4){
val <- "April"
} else if (month == "may" | month == 5){
val <- "May"
} else if (month == "june" | month == "jun" | month == 6){
val <- "June"
} else if (month == "july" | month == "jul" | month == 7){
val <- "July"
} else if (month == "august" | month == "aug" | month == 8){
val <- "August"
} else if (month == "september" | month == "sept" | month == "sep" | month == 9){
val <- "September"
} else if (month == "october" | month == "oct" | month == 10){
val <- "October"
} else if (month == "november" | month == "nov" | month == 11){
val <- "November"
} else if (month == "december" | month == "dec" | month == 12){
val <- "December"
} else {
stop("The given argument for month does not match an acceptible input.")
}
# replace
.data[[val]] <- monthData
# return
return(.data)
}