### Impute Median Examples Source: https://naniar.njtierney.com/reference/impute_median.html Examples demonstrating how to use impute_median on vectors and within dplyr pipelines. ```R vec <- rnorm(10) vec[sample(1:10, 3)] <- NA impute_median(vec) ``` ```R library(dplyr) dat <- tibble( num = rnorm(10), int = as.integer(rpois(10, 5)), fct = factor(LETTERS[1:10]) ) %>% mutate( across( everything(), \(x) set_prop_miss(x, prop = 0.25) ) ) dat ``` ```R dat %>% nabular() %>% mutate( num = impute_median(num), int = impute_median(int), ) ``` ```R dat %>% nabular() %>% mutate( across( where(is.numeric), impute_median ) ) ``` ```R dat %>% nabular() %>% mutate( across( c("num", "int"), impute_median ) ) ``` -------------------------------- ### Add Label Shadow Example Source: https://naniar.njtierney.com/reference/add_label_shadow.html Example of using add_label_shadow in a pipeline with airquality data to identify rows with missing values. ```R airquality %>% add_shadow(Ozone, Solar.R) %>% add_label_shadow() ``` -------------------------------- ### Example of gather_shadow with airquality dataset Source: https://naniar.njtierney.com/reference/gather_shadow.html Demonstrates the output format of gather_shadow using the built-in airquality dataset. ```R gather_shadow(airquality) #> # A tibble: 918 × 3 #> case variable missing #> #> 1 1 Ozone_NA !NA #> 2 1 Solar.R_NA !NA #> 3 1 Wind_NA !NA #> 4 1 Temp_NA !NA #> 5 1 Month_NA !NA #> 6 1 Day_NA !NA #> 7 2 Ozone_NA !NA #> 8 2 Solar.R_NA !NA #> 9 2 Wind_NA !NA #> 10 2 Temp_NA !NA #> # ℹ 908 more rows ``` -------------------------------- ### Example of recode_shadow with .where Source: https://naniar.njtierney.com/reference/where.html Demonstrates how to use .where inside recode_shadow to conditionally recode shadow variables. ```R if (FALSE) { # \dontrun{ df <- tibble::tribble( ~wind, ~temp, -99, 45, 68, NA, 72, 25 ) dfs <- bind_shadow(df) recode_shadow(dfs, temp = .where(wind == -99 ~ "bananas")) } # } ``` -------------------------------- ### Install naniar Development Version Source: https://naniar.njtierney.com/index.html Install the latest development version of the naniar package from GitHub using the remotes package. ```r # install.packages("remotes") remotes::install_github("njtierney/naniar") ``` -------------------------------- ### Install naniar from CRAN Source: https://naniar.njtierney.com/index.html Use this command to install the stable version of the naniar package from CRAN. ```r install.packages("naniar") ``` -------------------------------- ### Examples of cast_shadow_shift Source: https://naniar.njtierney.com/reference/cast_shadow_shift.html Demonstrates applying cast_shadow_shift to specific columns and using tidyselect helpers. ```R airquality %>% cast_shadow_shift(Ozone,Temp) airquality %>% cast_shadow_shift(dplyr::contains("o")) ``` -------------------------------- ### Recoding Shadow Matrix Examples Source: https://naniar.njtierney.com/reference/recode_shadow.html Demonstrates creating a shadow matrix with bind_shadow and applying custom labels to missing values using recode_shadow. ```R df <- tibble::tribble( ~wind, ~temp, -99, 45, 68, NA, 72, 25 ) dfs <- bind_shadow(df) dfs #> # A tibble: 3 × 4 #> wind temp wind_NA temp_NA #> #> 1 -99 45 !NA !NA #> 2 68 NA !NA NA #> 3 72 25 !NA !NA recode_shadow(dfs, temp = .where(wind == -99 ~ "bananas")) #> # A tibble: 3 × 4 #> wind temp wind_NA temp_NA #> #> 1 -99 45 !NA NA_bananas #> 2 68 NA !NA NA #> 3 72 25 !NA !NA recode_shadow(dfs, temp = .where(wind == -99 ~ "bananas")) %>% recode_shadow(wind = .where(wind == -99 ~ "apples")) #> # A tibble: 3 × 4 #> wind temp wind_NA temp_NA #> #> 1 -99 45 NA_apples NA_bananas #> 2 68 NA !NA NA #> 3 72 25 !NA !NA ``` -------------------------------- ### Example usage of proportion functions Source: https://naniar.njtierney.com/reference/pct-miss-complete-var.html Demonstrates calculating the proportion of variables with missing or complete values using the airquality dataset. ```R prop_miss_var(airquality) #> [1] 0.3333333 prop_complete_var(airquality) #> [1] 0.6666667 ``` -------------------------------- ### Example of `as_shadow_upset()` and `UpSetR::upset()` workflow Source: https://naniar.njtierney.com/news/index.html This code block shows the previous workflow for generating upset plots using `as_shadow_upset()` and `UpSetR::upset()`. ```R data %>% as_shadow_upset() %>% UpSetR::upset() ``` -------------------------------- ### Example of label_miss_1d Source: https://naniar.njtierney.com/reference/label_miss_1d.html Demonstrates labeling missing values in the Ozone column of the airquality dataset. ```R label_miss_1d(airquality$Ozone) #> [1] Not Missing Not Missing Not Missing Not Missing Missing Not Missing #> [7] Not Missing Not Missing Not Missing Missing Not Missing Not Missing #> [13] Not Missing Not Missing Not Missing Not Missing Not Missing Not Missing #> [19] Not Missing Not Missing Not Missing Not Missing Not Missing Not Missing #> [25] Missing Missing Missing Not Missing Not Missing Not Missing #> [31] Not Missing Missing Missing Missing Missing Missing #> [37] Missing Not Missing Missing Not Missing Not Missing Missing #> [43] Missing Not Missing Missing Missing Not Missing Not Missing #> [49] Not Missing Not Missing Not Missing Missing Missing Missing #> [55] Missing Missing Missing Missing Missing Missing #> [61] Missing Not Missing Not Missing Not Missing Missing Not Missing #> [67] Not Missing Not Missing Not Missing Not Missing Not Missing Missing #> [73] Not Missing Not Missing Missing Not Missing Not Missing Not Missing #> [79] Not Missing Not Missing Not Missing Not Missing Missing Missing #> [85] Not Missing Not Missing Not Missing Not Missing Not Missing Not Missing #> [91] Not Missing Not Missing Not Missing Not Missing Not Missing Not Missing #> [97] Not Missing Not Missing Not Missing Not Missing Not Missing Missing #> [103] Missing Not Missing Not Missing Not Missing Missing Not Missing #> [109] Not Missing Not Missing Not Missing Not Missing Not Missing Not Missing #> [115] Missing Not Missing Not Missing Not Missing Missing Not Missing #> [121] Not Missing Not Missing Not Missing Not Missing Not Missing Not Missing #> [127] Not Missing Not Missing Not Missing Not Missing Not Missing Not Missing #> [133] Not Missing Not Missing Not Missing Not Missing Not Missing Not Missing #> [139] Not Missing Not Missing Not Missing Not Missing Not Missing Not Missing #> [145] Not Missing Not Missing Not Missing Not Missing Not Missing Missing #> [151] Not Missing Not Missing Not Missing #> Levels: Missing Not Missing ``` -------------------------------- ### Examples of miss_summary Source: https://naniar.njtierney.com/reference/miss_summary.html Demonstrates how to access specific summary components from the returned object and how to use the function with grouped data. ```R s_miss <- miss_summary(airquality) s_miss$miss_df_prop #> [1] 0.04793028 s_miss$miss_case_table #> [[1]] #> # A tibble: 3 × 3 #> n_miss_in_case n_cases pct_cases #> #> 1 0 111 72.5 #> 2 1 40 26.1 #> 3 2 2 1.31 #> s_miss$miss_var_summary #> [[1]] #> # A tibble: 6 × 3 #> variable n_miss pct_miss #> #> 1 Ozone 37 24.2 #> 2 Solar.R 7 4.58 #> 3 Wind 0 0 #> 4 Temp 0 0 #> 5 Month 0 0 #> 6 Day 0 0 #> # etc, etc, etc. if (FALSE) { # \dontrun{ library(dplyr) s_miss_group <- group_by(airquality, Month) %>% miss_summary() s_miss_group$miss_df_prop s_miss_group$miss_case_table # etc, etc, etc. } # } ``` -------------------------------- ### Advanced gg_miss_case configurations Source: https://naniar.njtierney.com/reference/gg_miss_case.html Examples demonstrating customization of the plot using ggplot2 themes, percentage display, ordering, and faceting. ```R library(ggplot2) gg_miss_case(airquality) + labs(x = "Number of Cases") gg_miss_case(airquality, show_pct = TRUE) gg_miss_case(airquality, order_cases = FALSE) gg_miss_case(airquality, facet = Month) gg_miss_case(airquality, facet = Month, order_cases = FALSE) gg_miss_case(airquality, facet = Month, show_pct = TRUE) ``` -------------------------------- ### Examples of add_label_missings Source: https://naniar.njtierney.com/reference/add_label_missings.html Demonstrates labeling rows as missing or complete using the airquality dataset with default and custom labels. ```R airquality %>% add_label_missings() ``` ```R airquality %>% add_label_missings(Ozone, Solar.R) ``` ```R airquality %>% add_label_missings(Ozone, Solar.R, missing = "yes", complete = "no") ``` -------------------------------- ### Example of impute_zero Source: https://naniar.njtierney.com/reference/impute_zero.html Demonstrates replacing missing values in a numeric vector with zero and using it within a data pipeline. ```R vec <- rnorm(10) vec[sample(1:10, 3)] <- NA vec #> [1] NA -0.25705805 -1.41422789 0.01887104 0.35647301 0.89006961 #> [7] NA NA 0.43744452 -1.65606748 impute_zero(vec) #> [1] 0.00000000 -0.25705805 -1.41422789 0.01887104 0.35647301 0.89006961 #> [7] 0.00000000 0.00000000 0.43744452 -1.65606748 library(dplyr) dat <- tibble( num = rnorm(10), int = rpois(10, 5), fct = factor(LETTERS[1:10]) ) %>% mutate( across( everything(), \(x) set_prop_miss(x, prop = 0.25) ) ) dat #> # A tibble: 10 × 3 #> num int fct #> #> 1 -1.30 5 A #> 2 2.19 3 B #> 3 -0.303 NA C #> 4 1.36 2 NA #> 5 -0.744 5 E #> 6 NA 6 NA #> 7 1.76 7 G #> 8 0.724 NA H #> 9 NA 3 I #> 10 1.38 7 J dat %>% nabular() %>% mutate( num = impute_fixed(num, -9999), int = impute_zero(int), fct = impute_factor(fct, "out") ) #> # A tibble: 10 × 6 #> num int fct num_NA int_NA fct_NA #> #> 1 -1.30 5 A !NA !NA !NA #> 2 2.19 3 B !NA !NA !NA #> 3 -0.303 0 C !NA NA !NA #> 4 1.36 2 out !NA !NA NA #> 5 -0.744 5 E !NA !NA !NA #> 6 -9999 6 out NA !NA NA #> 7 1.76 7 G !NA !NA !NA #> 8 0.724 0 H !NA NA !NA #> 9 -9999 3 I NA !NA !NA #> 10 1.38 7 J !NA !NA !NA ``` -------------------------------- ### Examples of impute_mean usage Source: https://naniar.njtierney.com/reference/impute_mean.html Demonstrates applying impute_mean to a simple vector and within a dplyr pipeline using nabular data. ```R library(dplyr) vec <- rnorm(10) vec[sample(1:10, 3)] <- NA impute_mean(vec) #> [1] 0.5301633 0.7462801 1.3446716 0.5301633 -0.4860343 0.8088018 #> [7] 0.3218633 0.0581052 0.5301633 0.9174552 dat <- tibble( num = rnorm(10), int = as.integer(rpois(10, 5)), fct = factor(LETTERS[1:10]) ) %>% mutate( across( everything(), \(x) set_prop_miss(x, prop = 0.25) ) ) dat #> # A tibble: 10 × 3 #> num int fct #> #> 1 NA 7 A #> 2 1.35 NA B #> 3 NA 4 C #> 4 0.590 4 NA #> 5 1.23 5 E #> 6 -1.42 NA F #> 7 -1.04 9 NA #> 8 1.28 3 H #> 9 -1.31 7 I #> 10 1.60 6 J dat %>% nabular() %>% mutate( num = impute_mean(num), int = impute_mean(int), fct = impute_mean(fct), ) #> # A tibble: 10 × 6 #> num int fct num_NA int_NA fct_NA #> #> 1 0.285 7 A NA !NA !NA #> 2 1.35 5.62 B !NA NA !NA #> 3 0.285 4 C NA !NA !NA #> 4 0.590 4 J !NA !NA NA #> 5 1.23 5 E !NA !NA !NA #> 6 -1.42 5.62 F !NA NA !NA #> 7 -1.04 9 J !NA !NA NA #> 8 1.28 3 H !NA !NA !NA #> 9 -1.31 7 I !NA !NA !NA #> 10 1.60 6 J !NA !NA !NA dat %>% nabular() %>% mutate( across( where(is.numeric), impute_mean ) ) #> # A tibble: 10 × 6 #> num int fct num_NA int_NA fct_NA #> #> 1 0.285 7 A NA !NA !NA #> 2 1.35 5.62 B !NA NA !NA #> 3 0.285 4 C NA !NA !NA #> 4 0.590 4 NA !NA !NA NA #> 5 1.23 5 E !NA !NA !NA #> 6 -1.42 5.62 F !NA NA !NA #> 7 -1.04 9 NA !NA !NA NA #> 8 1.28 3 H !NA !NA !NA #> 9 -1.31 7 I !NA !NA !NA #> 10 1.60 6 J !NA !NA !NA dat %>% nabular() %>% mutate( across( c("num", "int"), impute_mean ) ) #> # A tibble: 10 × 6 #> num int fct num_NA int_NA fct_NA #> #> 1 0.285 7 A NA !NA !NA #> 2 1.35 5.62 B !NA NA !NA #> 3 0.285 4 C NA !NA !NA #> 4 0.590 4 NA !NA !NA NA #> 5 1.23 5 E !NA !NA !NA #> 6 -1.42 5.62 F !NA NA !NA #> 7 -1.04 9 NA !NA !NA NA #> 8 1.28 3 H !NA !NA !NA #> 9 -1.31 7 I !NA !NA !NA #> 10 1.60 6 J !NA !NA !NA ``` -------------------------------- ### Create Example Data Frame Source: https://naniar.njtierney.com/articles/replace-with-na.html Creates a sample tibble with various representations of missing values, including strings and numeric codes, for demonstrating NA replacement. ```r df <- tibble::tribble( ~name, ~x, ~y, ~z, "N/A", 1, "N/A", -100, "N A", 3, "NOt available", -99, "N / A", NA, "29", -98, "Not Available", -99, "25", -101, "John Smith", -98, "28", -1) ``` -------------------------------- ### Examples of pct_miss Source: https://naniar.njtierney.com/reference/pct_miss.html Demonstrates calculating the percentage of missing values for a data frame and a specific vector. ```R pct_miss(airquality) #> [1] 4.793028 pct_miss(airquality$Ozone) #> [1] 24.18301 ``` -------------------------------- ### Examples of miss_scan_count Source: https://naniar.njtierney.com/reference/miss_scan_count.html Demonstrates searching for numeric values, character strings, and special characters using regular expressions. ```R dat_ms <- tibble::tribble(~x, ~y, ~z, ~specials, 1, "A", -100, "?", 3, "N/A", -99, "!", NA, NA, -98, ".", -99, "E", -101, "*", -98, "F", -1, "-") miss_scan_count(dat_ms,-99) #> # A tibble: 4 × 2 #> Variable n #> #> 1 x 1 #> 2 y 0 #> 3 z 1 #> 4 specials 0 miss_scan_count(dat_ms,c(-99,-98)) #> # A tibble: 4 × 2 #> Variable n #> #> 1 x 2 #> 2 y 0 #> 3 z 2 #> 4 specials 0 miss_scan_count(dat_ms,c("-99","-98","N/A")) #> # A tibble: 4 × 2 #> Variable n #> #> 1 x 2 #> 2 y 1 #> 3 z 2 #> 4 specials 0 miss_scan_count(dat_ms, "\\?") #> # A tibble: 4 × 2 #> Variable n #> #> 1 x 0 #> 2 y 0 #> 3 z 0 #> 4 specials 1 miss_scan_count(dat_ms, "\\!") #> # A tibble: 4 × 2 #> Variable n #> #> 1 x 0 #> 2 y 0 #> 3 z 0 #> 4 specials 1 miss_scan_count(dat_ms, "\\.") #> # A tibble: 4 × 2 #> Variable n #> #> 1 x 0 #> 2 y 0 #> 3 z 0 #> 4 specials 1 miss_scan_count(dat_ms, "\\*") #> # A tibble: 4 × 2 #> Variable n #> #> 1 x 0 #> 2 y 0 #> 3 z 0 #> 4 specials 1 miss_scan_count(dat_ms, "-") #> # A tibble: 4 × 2 #> Variable n #> #> 1 x 2 #> 2 y 0 #> 3 z 5 #> 4 specials 1 miss_scan_count(dat_ms,common_na_strings) #> # A tibble: 4 × 2 #> Variable n #> #> 1 x 4 #> 2 y 4 #> 3 z 5 #> 4 specials 5 ``` -------------------------------- ### Impute Mode Examples Source: https://naniar.njtierney.com/reference/impute_mode.html Demonstration of imputing missing values in a numeric vector and within a tibble using naniar's nabular format. ```R vec <- rnorm(10) vec[sample(1:10, 3)] <- NA impute_mode(vec) library(dplyr) dat <- tibble( num = rnorm(10), int = rpois(10, 5), fct = factor(LETTERS[1:10]) ) %>% mutate( across( everything(), \(x) set_prop_miss(x, prop = 0.25) ) ) dat dat %>% nabular() %>% mutate( num = impute_mode(num), int = impute_mode(int), fct = impute_mode(fct) ) ``` -------------------------------- ### Demonstrate na_if length constraint Source: https://naniar.njtierney.com/articles/replace-with-na.html Example showing that dplyr::na_if cannot accept vectors of length greater than one. ```R # Not run: df_4 <- df %>% dplyr::na_if(x = ., y = na_strings) # Error in check_length(y, x, fmt_args("y"), glue("same as {fmt_args(~x)}")) : # argument "y" is missing, with no default ``` -------------------------------- ### Explore Missing Data Visually with ggplot2 Source: https://naniar.njtierney.com/reference/bind_shadow.html This example demonstrates how to use the output of `bind_shadow` with `ggplot2` to visualize missing data. It facets the histogram of Ozone by whether Solar Radiation is missing or not. ```R library(ggplot2) # using the bounded shadow to visualise Ozone according to whether Solar # Radiation is missing or not. ggplot(data = aq_shadow, aes(x = Ozone)) + geom_histogram() + facet_wrap(~Solar.R_NA, ncol = 1) ``` -------------------------------- ### Replace values with NA using naniar Source: https://naniar.njtierney.com/reference/replace_with_na_at.html Examples demonstrating how to replace values with NA in a tibble based on single or multiple column conditions. ```R dat_ms <- tibble::tribble(~x, ~y, ~z, 1, "A", -100, 3, "N/A", -99, NA, NA, -98, -99, "E", -101, -98, "F", -1) dat_ms #> # A tibble: 5 × 3 #> x y z #> #> 1 1 A -100 #> 2 3 N/A -99 #> 3 NA NA -98 #> 4 -99 E -101 #> 5 -98 F -1 replace_with_na_at(data = dat_ms, .vars = "x", condition = ~.x == -99) #> # A tibble: 5 × 3 #> x y z #> #> 1 1 A -100 #> 2 3 N/A -99 #> 3 NA NA -98 #> 4 NA E -101 #> 5 -98 F -1 replace_with_na_at(data = dat_ms, .vars = c("x","z"), condition = ~.x == -99) #> # A tibble: 5 × 3 #> x y z #> #> 1 1 A -100 #> 2 3 N/A NA #> 3 NA NA -98 #> 4 NA E -101 #> 5 -98 F -1 # replace using values in common_na_strings replace_with_na_at(data = dat_ms, .vars = c("x","z"), condition = ~.x %in% common_na_strings) #> # A tibble: 5 × 3 #> x y z #> #> 1 1 A -100 #> 2 3 N/A -99 #> 3 NA NA -98 #> 4 -99 E -101 #> 5 -98 F -1 ``` -------------------------------- ### Explore Missingness with ggplot2 Source: https://naniar.njtierney.com/reference/riskfactors.html This example demonstrates how to use ggplot2 with geom_miss_point to visualize the relationship between two variables while highlighting missing data points. It also shows how to use faceting to explore missingness across different categories. ```R library(ggplot2) p <- ggplot(riskfactors, aes(x = health_poor, y = bmi)) + geom_miss_point() p ``` ```R p + facet_wrap(~sex) ``` ```R p + facet_wrap(~education) ``` -------------------------------- ### Examples of replacing values with NA Source: https://naniar.njtierney.com/reference/replace_with_na.html Demonstrates replacing values in specific columns using a named list. The input data frame is defined using tibble::tribble. ```R dat_ms <- tibble::tribble(~x, ~y, ~z, 1, "A", -100, 3, "N/A", -99, NA, NA, -98, -99, "E", -101, -98, "F", -1) replace_with_na(dat_ms, replace = list(x = -99)) replace_with_na(dat_ms, replace = list(x = c(-99, -98))) replace_with_na(dat_ms, replace = list(x = c(-99, -98), y = c("N/A"), z = c(-101))) ``` -------------------------------- ### Summarise Missing Values by Month Span Source: https://naniar.njtierney.com/reference/miss_var_span.html This example demonstrates grouping by month before applying miss_var_span to analyze missing values within hourly counts for each month, with a span of 168 hours. ```R library(dplyr) pedestrian %>% group_by(month) %>% miss_var_span(var = hourly_counts, span_every = 168) ``` -------------------------------- ### Raw Data Snippet Source: https://naniar.njtierney.com/reference/add_n_miss.html This is a raw data snippet, likely from a file or output, showing numerical and NA values. It serves as an example of data that might be processed by analysis tools. ```text #> 129 32 92 15.5 84 9 6 0 #> 130 20 252 10.9 80 9 7 0 #> 131 23 220 10.3 78 9 8 0 #> 132 21 230 10.9 75 9 9 0 #> 133 24 259 9.7 73 9 10 0 #> 134 44 236 14.9 81 9 11 0 #> 135 21 259 15.5 76 9 12 0 #> 136 28 238 6.3 77 9 13 0 #> 137 9 24 10.9 71 9 14 0 #> 138 13 112 11.5 71 9 15 0 #> 139 46 237 6.9 78 9 16 0 #> 140 18 224 13.8 67 9 17 0 #> 141 13 27 10.3 76 9 18 0 #> 142 24 238 10.3 68 9 19 0 #> 143 16 201 8.0 82 9 20 0 #> 144 13 238 12.6 64 9 21 0 #> 145 23 14 9.2 71 9 22 0 #> 146 36 139 10.3 81 9 23 0 #> 147 7 49 10.3 69 9 24 0 #> 148 14 20 16.6 63 9 25 0 #> 149 30 193 6.9 70 9 26 0 #> 150 NA 145 13.2 77 9 27 1 #> 151 14 191 14.3 75 9 28 0 #> 152 18 131 8.0 76 9 29 0 #> 153 20 223 11.5 68 9 30 0 ``` -------------------------------- ### Visualize Missing Data with vis_miss() Source: https://naniar.njtierney.com/articles/naniar-visualisation.html Use vis_miss() to get a visual overview of missing data in a dataset. It highlights missing values in black and provides overall missing percentage information. ```R library(naniar) vis_miss(airquality) ``` -------------------------------- ### Visualize Missing Data Overview Source: https://naniar.njtierney.com/reference/riskfactors.html Use vis_miss to get a general overview of missing data in the dataset. This function provides a visual representation of missing values across variables. ```R vis_miss(riskfactors) ``` -------------------------------- ### Clone the repository Source: https://naniar.njtierney.com/CONTRIBUTING.html Initial step to create a local copy of the repository from your GitHub account. ```bash git clone https://github.com//{repo}.git ``` -------------------------------- ### GET /prop_complete_case Source: https://naniar.njtierney.com/reference/prop-miss-complete-case.html Calculates the proportion of cases (rows) that contain complete values. ```APIDOC ## GET /prop_complete_case ### Description Calculate the proportion of cases (rows) that contain complete values. ### Method GET ### Endpoint prop_complete_case(data) ### Parameters #### Request Body - **data** (dataframe) - Required - A dataframe to analyze. ### Response #### Success Response (200) - **value** (numeric) - The proportion of cases that contain a complete value. ### Request Example prop_complete_case(airquality) ### Response Example 0.7254902 ``` -------------------------------- ### Implement and Verify `shade` Values Source: https://naniar.njtierney.com/news/index.html This section introduces the concept of `shade` values and provides verification functions like `is_shade`, `are_shade`, and `which_are_shade`. ```R is_shade() are_shade() which_are_shade() ``` -------------------------------- ### GET /prop_miss_case Source: https://naniar.njtierney.com/reference/prop-miss-complete-case.html Calculates the proportion of cases (rows) that contain missing values. ```APIDOC ## GET /prop_miss_case ### Description Calculate the proportion of cases (rows) that contain missing values. ### Method GET ### Endpoint prop_miss_case(data) ### Parameters #### Request Body - **data** (dataframe) - Required - A dataframe to analyze. ### Response #### Success Response (200) - **value** (numeric) - The proportion of cases that contain a missing value. ### Request Example prop_miss_case(airquality) ### Response Example 0.2745098 ``` -------------------------------- ### Create a toy dataset Source: https://naniar.njtierney.com/articles/special-missing-values.html Initializes a tibble with specific values to demonstrate missingness handling. ```R df <- tibble::tribble( ~wind, ~temp, -99, 45, 68, NA, 72, 25 ) df ``` -------------------------------- ### GET miss_prop_summary Source: https://naniar.njtierney.com/reference/miss_prop_summary.html Calculates the proportion of missing values in a dataframe, variables, and cases. ```APIDOC ## GET miss_prop_summary ### Description Returns missing data information about the dataframe, the variables, and the cases. It calculates the proportion of elements containing missing values at the dataframe level, variable level, and case level. ### Method GET ### Endpoint miss_prop_summary(data) ### Parameters #### Path Parameters - **data** (dataframe) - Required - A dataframe object to analyze for missing values. ### Request Example miss_prop_summary(airquality) ### Response #### Success Response (200) - **df** (double) - Proportion of missing values in the entire dataframe. - **var** (double) - Proportion of missing values across variables. - **case** (double) - Proportion of missing values across cases. #### Response Example # A tibble: 1 × 3 # df var case # # 1 0.0479 0.333 0.275 ``` -------------------------------- ### GET gg_miss_which Source: https://naniar.njtierney.com/reference/gg_miss_which.html Visualizes which variables in a dataframe contain missing values using a rectangle-based plot. ```APIDOC ## gg_miss_which ### Description This function produces a set of rectangles indicating whether there is a missing element in a column or not. It returns a ggplot object. ### Arguments - **x** (dataframe) - Required - The dataframe to analyze for missing values. ### Request Example ```r gg_miss_which(airquality) ``` ### Response - **ggplot object** - A visualization showing missing value distribution across variables. ``` -------------------------------- ### Sync with upstream Source: https://naniar.njtierney.com/CONTRIBUTING.html Commands to fetch and merge changes from the upstream repository. ```bash git fetch upstream ``` ```bash git pull upstream ``` -------------------------------- ### Configure upstream remote Source: https://naniar.njtierney.com/CONTRIBUTING.html Set up the upstream remote to track changes from the original repository. ```bash git remote add upstream https://github.com/{owner}/{repo}.git ``` -------------------------------- ### Verify Data Equality Source: https://naniar.njtierney.com/articles/naniar.html Confirms that bind_shadow and nabular produce identical results. ```R all.equal(aq_shadow, aq_nab) ``` -------------------------------- ### Get Variable Names with Missing Values Source: https://naniar.njtierney.com/news/index.html The `miss_var_which()` function returns a vector of variable names that contain missing values. ```R miss_var_which(data) ``` -------------------------------- ### Visualize missing data spans Source: https://naniar.njtierney.com/reference/gg_miss_span.html Examples of using gg_miss_span to visualize missing data, including faceting and ggplot2 theme customization. ```R library(ggplot2) gg_miss_span(pedestrian, hourly_counts, span_every = 3000) gg_miss_span(pedestrian, hourly_counts, span_every = 3000, facet = sensor_name) # works with the rest of ggplot gg_miss_span(pedestrian, hourly_counts, span_every = 3000) + labs(x = "custom") gg_miss_span(pedestrian, hourly_counts, span_every = 3000) + theme_dark() ``` -------------------------------- ### Create nabular data Source: https://naniar.njtierney.com/articles/special-missing-values.html Uses bind_shadow to create a shadow matrix for the dataset. ```R dfs <- bind_shadow(df) dfs ``` -------------------------------- ### Find Missing Values in Dataframe Source: https://naniar.njtierney.com/reference/which_na.html Use `which_na` to get the integer locations of missing values in a dataframe. This function is an alias for `which(is.na())`. ```r which_na(airquality) #> [1] 5 10 25 26 27 32 33 34 35 36 37 39 42 43 45 46 52 53 54 #> [20] 55 56 57 58 59 60 61 65 72 75 83 84 102 103 107 115 119 150 158 #> [39] 159 164 180 249 250 251 ``` -------------------------------- ### Prepare Data for UpSetR Plotting Source: https://naniar.njtierney.com/news/index.html Use `as_shadow_upset()` to format data for plotting with `UpSetR`. This function prepares the data by converting it into a suitable structure for visualizing set intersections of missing values. ```R airquality %>$ as_shadow_upset() %>$ UpSetR::upset() ``` -------------------------------- ### Visualize Missing Data with vis_miss Source: https://naniar.njtierney.com/reference/pedestrian.html Visualizes the missingness pattern in the pedestrian dataset. Use this to get a general overview of where missing values occur. ```r vis_miss(pedestrian) ``` -------------------------------- ### Bind shadow data to create nabular format Source: https://naniar.njtierney.com/index.html Use bind_shadow or nabular to append shadow variables to the original dataset, creating a nabular format for tracking missingness. ```R bind_shadow(airquality) nabular(airquality) ``` -------------------------------- ### Impute Missing Values with naniar Source: https://naniar.njtierney.com/reference/scoped-impute_mean.html Demonstrates how to impute missing values using mean imputation based on specific variables or predicates. ```R library(dplyr) impute_mean_at(airquality, .vars = vars(Ozone)) impute_mean_if(airquality, .predicate = is.numeric) ``` -------------------------------- ### Impute Median Function Source: https://naniar.njtierney.com/reference/impute_median.html This function imputes the median value into a vector with missing values. It handles different data types and provides examples for its usage. ```APIDOC ## Impute Median Function ### Description Impute the median value into a vector with missing values. ### Usage ```R impute_median(x) # Default S3 method impute_median(x) # S3 method for class 'factor' impute_median(x) ``` ### Arguments * **x** (vector) - The input vector that may contain missing values. ### Value A vector with median values imputed for any missing entries. ### Examples ```R # Example 1: Basic usage with a numeric vector vec <- rnorm(10) vec[sample(1:10, 3)] <- NA impute_median(vec) # Example 2: Using with dplyr and tibble library(dplyr) dat <- tibble( num = rnorm(10), int = as.integer(rpois(10, 5)), fct = factor(LETTERS[1:10]) ) %>% mutate( across( everything(), ~ set_prop_miss(.x, prop = 0.25) ) ) # Impute median for specific numeric columns dat %>% nabular() %>% mutate( num = impute_median(num), int = impute_median(int) ) # Impute median for all numeric columns dat %>% nabular() %>% mutate( across( where(is.numeric), impute_median ) ) # Impute median for a subset of numeric columns dat %>% nabular() %>% mutate( across( c("num", "int"), impute_median ) ) ``` ```