-
Notifications
You must be signed in to change notification settings - Fork 2
/
flagAbsent.R
52 lines (47 loc) · 1.63 KB
/
flagAbsent.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
# This function is a modification of a bdc function and flags columns as FALSE when they are marked
# as "ABSENT". This function was written on the 22nd of May 2022 by James Dorey. Email James at
# jbdorey[at]me.com for help.
#' Flags occurrences that are marked as absent
#'
#' Flags occurrences that are "ABSENT" for the occurrenceStatus (or some other user-specified) column.
#'
#' @param data A data frame or tibble. Occurrence records as input.
#' @param PresAbs Character. The column in which the function will find "ABSENT" or "PRESENT" records.
#' Default = "occurrenceStatus"
#'
#' @return The input data with a new column called ".occurrenceAbsent" where FALSE == "ABSENT" records.
#' @export
#'
#'@importFrom dplyr %>%
#'
#' @examples
#' # Bring in the data
#' data(beesRaw)
#' # Run the function
#' beesRaw_out <- flagAbsent(data = beesRaw,
#' PresAbs = "occurrenceStatus")
#' # See the result
#' table(beesRaw_out$.occurrenceAbsent, useNA = "always")
flagAbsent <-
function(data = NULL,
PresAbs = "occurrenceStatus") {
.data <- .occurrenceAbsent <- NULL
requireNamespace("dplyr")
# Make a new column called .occurrenceAbsent to be TRUE when occurrenceStatus is "present" or NA
data <-
data %>%
dplyr::mutate(
.occurrenceAbsent =
!.data[[PresAbs]] %in% c("ABSENT"))
# Return user output
message(
paste(
"\\.occurrenceAbsent:\n",
"Flagged",
format(sum(data$.occurrenceAbsent == FALSE, na.rm = TRUE), big.mark = ","),
"absent records:\n",
"One column was added to the database.\n"
)
)
return(data)
}