-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_flows.R
50 lines (43 loc) · 1.58 KB
/
clean_flows.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
#' Clean Flow Record
#'
#' Function to replace zeros in the flow record with specified value and replace negative discharge values with \code{NA}.
#' @param discharge numeric vector of discharges.
#' @param replace_0 numeric value or \code{NA} to replace zeros with. Defaults to 0.001.
#' @param replace_neg numeric value or \code{NA} to replace negative values with. Defaults to \code{NA}.
#'
#' @return numerioc vector same length as values provided in \code{discharge}.
#' @export
clean_flows <- function(discharge,
replace_0 = 0.001,
replace_neg = NA) {
## check discharge is numeric
if (!is.numeric(discharge)) {
stop("'discharge' should be a numeric vector")
}
## check replace_0 is numeric or NA
if (!is.numeric(replace_0)) {
if (!is.na(replace_0)) {
stop("'replace_0' must be numeric value between 0 and 10 or NA")
}
}
## check replace_0 is appropriate value
if (replace_0 < 0 & !is.na(replace_0) ) {
stop("'replace_0' must be numeric value between 0 and 10 or NA")
}
if (replace_0 > 10 & !is.na(replace_0) ) {
stop("'replace_0' must be numeric value between 0 and 10 or NA")
}
## check replace_neg is numeric or NA
if (!is.numeric(replace_neg)) {
if (!is.na(replace_neg)) {
stop("'replace_neg' must be numeric value or NA")
}
}
## replace zero discharges with replace_0
discharge_0 <- discharge == 0
discharge[discharge_0] <- replace_0
## replace negative flows with neg_flows value
discharge_neg <- discharge < 0
discharge[discharge_neg] <- replace_neg
return(discharge)
}