-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathshade.R
174 lines (148 loc) · 3.95 KB
/
shade.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
#' Create a new shade factor
#'
#' @param x a factor to convert into a `shade` object
#' @param extra_levels the extra levels to give to `shade` objects, such as "broken_machine" and so on, which get converted into "NA_broken_machine".
#'
#' @return a new shade, which is built upon a factor
#' @keywords internal
#' @noRd
new_shade <- function(x, extra_levels = NULL){
if (!is.factor(x)) {
rlang::abort(msg = "input to shade must be a factor")
}
levels(x) <- union(levels(x), glue::glue("NA_{extra_levels}"))
structure(x,
class = c("shade", "factor"))
}
#' Detect if this is a shade
#'
#' This tells us if this column is a shade
#'
#' @param x a vector you want to test if is a shade
#'
#' @return logical - is this a shade?
#' @export
#' @name is_shade
#'
#' @examples
#'
#' xs <- shade(c(NA, 1, 2, "3"))
#'
#' is_shade(xs)
#' are_shade(xs)
#' any_shade(xs)
#'
#' aq_s <- as_shadow(airquality)
#'
#' is_shade(aq_s)
#' are_shade(aq_s)
#' any_shade(aq_s)
#' any_shade(airquality)
#'
#'
is_shade <- function(x){
inherits(x, "shade")
}
#' @export
#' @rdname is_shade
are_shade <- function(x){
purrr::map(x, class) %>%
purrr::map_lgl(~any(grepl("shade",.)))
}
#' @export
#' @rdname is_shade
any_shade <- function(x){
any(are_shade(x))
}
#' Create new levels of missing
#'
#' Returns (at least) factors of !NA and NA, where !NA indicates a datum that is
#' not missing, and NA indicates missingness. It also allows you to specify
#' some new missings, if you like. This function is what powers the factor
#' levels in `as_shadow()`.
#'
#' @param x a vector
#' @param ... additional levels of missing to add
#' @param extra_levels extra levels you might to specify for the factor.
#'
#' @examples
#' df <- tibble::tribble(
#' ~wind, ~temp,
#' -99, 45,
#' 68, NA,
#' 72, 25
#' )
#'
#' shade(df$wind)
#'
#' shade(df$wind, inst_fail = -99)
#'
#' @export
shade <- function(x, ..., extra_levels = NULL){
test_if_null(x)
if (length(x) == 0) {
rlang::abort(message = "input to shade must have length > 0")
}
# is list column
if (missing(...) & is.list(x)) {
x <- factor(purrr::map_lgl(x, ~length(.x)==0),
labels = c("!NA", "NA"),
levels = c(FALSE, TRUE))
return(new_shade(x, extra_levels))
}
if (!missing(...) & is.list(x)) {
rlang::abort(message = "additional levels of missing are not available when shade-ing lists column")
}
# if no other levels are specified
if (missing(...)) {
x <- factor(is.na(x),
labels = c("!NA", "NA"),
levels = c(FALSE, TRUE))
return(new_shade(x, extra_levels))
}
# if additional levels are specified
if (!missing(...)) {
# capture the dots
dict <- rlang::dots_list(...)
# which values of x match the specified values
match_pos <- match(x, dict)
# the name of the new NA level
custom_na_names <- paste0("NA_", names(dict))
# add exception for when there is no matches anywhere
# so skip this if there are no matches
if (!all_na(match_pos)) {
x[!is.na(match_pos)] <- custom_na_names[match_pos[!is.na(match_pos)]]
}
# add labels to those values that are missing
# For those values that were not matched
values_not_matched <- is.na(x[is.na(match_pos)])
# find if they were missing
x[is.na(match_pos)] <- ifelse(test = values_not_matched,
yes = "NA",
no = "!NA")
x <- factor(x,
levels = c("!NA", "NA", custom_na_names))
}
# and return a new shade value
new_shade(x, extra_levels)
}
#' Which variables are shades?
#'
#' This function tells us which variables contain shade information
#'
#' @param .tbl a data.frame or tbl
#'
#' @return numeric - which column numbers contain shade information
#'
#' @examples
#'
#' df_shadow <- bind_shadow(airquality)
#'
#' which_are_shade(df_shadow)
#'
#' @export
which_are_shade <- function(.tbl){
test_if_null(.tbl)
test_if_dataframe(.tbl)
which(are_shade(.tbl))
}