|
| 1 | + |
| 2 | +# give NAs a more meaningful label |
| 3 | +is_na <- function(x) { |
| 4 | + factor(is.na(x), levels = c(FALSE, TRUE), labels = c("!NA", "NA")) |
| 5 | +} |
| 6 | + |
| 7 | +# append some shadow cols |
| 8 | + |
| 9 | +# return a tibble that is a shadow matrix form. |
| 10 | + |
| 11 | +as_shadow <- function(data){ |
| 12 | + |
| 13 | + data_shadow <- purrr::map_df(data, is_na) |
| 14 | + |
| 15 | + names(data_shadow) <- paste0(names(data),"_NA") |
| 16 | + |
| 17 | + data_shadow |
| 18 | + |
| 19 | +} |
| 20 | + |
| 21 | +# as_shadow(airquality) |
| 22 | + |
| 23 | +bind_shadow <- function(data){ |
| 24 | + |
| 25 | + # data_shadow <- map_df(data, is_na) |
| 26 | + # names(data_shadow) <- paste0(names(data),"_NA") |
| 27 | + data_shadow <- as_shadow(data) |
| 28 | + |
| 29 | + bound_shadow <- tibble::as_tibble(dplyr::bind_cols(data, data_shadow)) |
| 30 | + |
| 31 | + bound_shadow |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +ggplot(data = bind_shadow(airquality), |
| 36 | + aes(x = Ozone)) + |
| 37 | + geom_histogram() + |
| 38 | + facet_wrap(~Solar.R_NA, |
| 39 | + ncol = 1) |
| 40 | +# |
| 41 | +ggplot(data = airquality, |
| 42 | + aes(x = Ozone)) + |
| 43 | + geom_histogram() + |
| 44 | + facet_wrap(~is_na(Solar.R), |
| 45 | + ncol = 1) |
| 46 | +# |
| 47 | +# ggplot(bind_shadow(airquality), |
| 48 | +# aes(x = Ozone, |
| 49 | +# colour = Solar.R_NA)) + |
| 50 | +# geom_density() + |
| 51 | +# geom_rug() |
| 52 | +# |
| 53 | +# ggplot(airquality, |
| 54 | +# aes(x = Ozone, |
| 55 | +# colour = is_na(Solar.R))) + |
| 56 | +# geom_density() + |
| 57 | +# geom_rug() |
| 58 | + |
| 59 | +# other "long" shadow format |
| 60 | + |
| 61 | +# shadow_join exists as we want to include this extra metadata about the rows that have missing data, but I also wanted to include some extra information about the class of the data, in case we need to gather the data back into a wider, rather than long, format. Here it takes a function `visdat`, `visdat:::fingerprint`, which is currently not a particularly complex function. |
| 62 | + |
| 63 | +gather_shadow <- function(df){ |
| 64 | + |
| 65 | + df_val_type <- df %>% |
| 66 | + as_data_frame() %>% |
| 67 | + purrr::dmap(visdat:::fingerprint) %>% |
| 68 | + mutate(rows = row_number()) %>% |
| 69 | + tidyr::gather_(key_col = "variable", |
| 70 | + value_col = "valueType", |
| 71 | + gather_cols = names(.)[-length(.)]) |
| 72 | + |
| 73 | + # df_shadow |
| 74 | + df_shadow <- df %>% |
| 75 | + as_data_frame() %>% |
| 76 | + mutate(rows = row_number()) %>% |
| 77 | + gather(key = variable, |
| 78 | + value = value, |
| 79 | + -rows) %>% |
| 80 | + mutate(shadow_matrix = is_na(value)) %>% |
| 81 | + left_join(df_val_type) |
| 82 | + |
| 83 | + # perhaps define some attributes |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +aq_shadow <- gather_shadow(airquality) |
0 commit comments