项目作者: Nelson-Gon

项目描述 :
mde: Missing Data Explorer
高级语言: R
项目地址: git://github.com/Nelson-Gon/mde.git
创建时间: 2019-10-26T14:18:34Z
项目社区:https://github.com/Nelson-Gon/mde

开源协议:GNU General Public License v3.0

下载


mde: Missing Data Explorer

2022-01-31

DOI
CRAN_Status_Badge
CRAN_Release_Badge
Codecov test
coverage
R-CMD-check
test-coverage
Project
Status
lifecycle
license
Downloads
TotalDownloads
GitHub last
commit
GitHub
issues
GitHub
issues-closed
PRs
Welcome
Maintenance

The goal of mde is to ease exploration of missingness.

Installation

CRAN release

  1. install.packages("mde")

Stable Development version

  1. devtools::install_github("Nelson-Gon/mde")
  2. devtools::install_github("Nelson-Gon/mde", build_vignettes=TRUE)

Unstable Development version

  1. devtools::install_github("Nelson-Gon/mde@develop")

Loading the package

  1. library(mde)
  2. #> Welcome to mde. This is mde version 0.3.2.
  3. #> Please file issues and feedback at https://www.github.com/Nelson-Gon/mde/issues
  4. #> Turn this message off using 'suppressPackageStartupMessages(library(mde))'
  5. #> Happy Exploration :)

Exploring missingness

To get a simple missingness report, use na_summary:

  1. na_summary(airquality)
  2. #> variable missing complete percent_complete percent_missing
  3. #> 1 Day 0 153 100.00000 0.000000
  4. #> 2 Month 0 153 100.00000 0.000000
  5. #> 3 Ozone 37 116 75.81699 24.183007
  6. #> 4 Solar.R 7 146 95.42484 4.575163
  7. #> 5 Temp 0 153 100.00000 0.000000
  8. #> 6 Wind 0 153 100.00000 0.000000

To sort this summary by a given column :

  1. na_summary(airquality,sort_by = "percent_complete")
  2. #> variable missing complete percent_complete percent_missing
  3. #> 3 Ozone 37 116 75.81699 24.183007
  4. #> 4 Solar.R 7 146 95.42484 4.575163
  5. #> 1 Day 0 153 100.00000 0.000000
  6. #> 2 Month 0 153 100.00000 0.000000
  7. #> 5 Temp 0 153 100.00000 0.000000
  8. #> 6 Wind 0 153 100.00000 0.000000

If one would like to reset (drop) row names, then one can set
row_names to TRUE This may especially be useful in cases where
rownames are simply numeric and do not have much additional use.

  1. na_summary(airquality,sort_by = "percent_complete", reset_rownames = TRUE)
  2. #> variable missing complete percent_complete percent_missing
  3. #> 1 Ozone 37 116 75.81699 24.183007
  4. #> 2 Solar.R 7 146 95.42484 4.575163
  5. #> 3 Day 0 153 100.00000 0.000000
  6. #> 4 Month 0 153 100.00000 0.000000
  7. #> 5 Temp 0 153 100.00000 0.000000
  8. #> 6 Wind 0 153 100.00000 0.000000

To sort by percent_missing instead:

  1. na_summary(airquality, sort_by = "percent_missing")
  2. #> variable missing complete percent_complete percent_missing
  3. #> 1 Day 0 153 100.00000 0.000000
  4. #> 2 Month 0 153 100.00000 0.000000
  5. #> 5 Temp 0 153 100.00000 0.000000
  6. #> 6 Wind 0 153 100.00000 0.000000
  7. #> 4 Solar.R 7 146 95.42484 4.575163
  8. #> 3 Ozone 37 116 75.81699 24.183007

To sort the above in descending order:

  1. na_summary(airquality, sort_by="percent_missing", descending = TRUE)
  2. #> variable missing complete percent_complete percent_missing
  3. #> 3 Ozone 37 116 75.81699 24.183007
  4. #> 4 Solar.R 7 146 95.42484 4.575163
  5. #> 1 Day 0 153 100.00000 0.000000
  6. #> 2 Month 0 153 100.00000 0.000000
  7. #> 5 Temp 0 153 100.00000 0.000000
  8. #> 6 Wind 0 153 100.00000 0.000000

To exclude certain columns from the analysis:

  1. na_summary(airquality, exclude_cols = c("Day", "Wind"))
  2. #> variable missing complete percent_complete percent_missing
  3. #> 1 Month 0 153 100.00000 0.000000
  4. #> 2 Ozone 37 116 75.81699 24.183007
  5. #> 3 Solar.R 7 146 95.42484 4.575163
  6. #> 4 Temp 0 153 100.00000 0.000000

To include or exclude via regex match:

  1. na_summary(airquality, regex_kind = "inclusion",pattern_type = "starts_with", pattern = "O|S")
  2. #> variable missing complete percent_complete percent_missing
  3. #> 1 Ozone 37 116 75.81699 24.183007
  4. #> 2 Solar.R 7 146 95.42484 4.575163
  1. na_summary(airquality, regex_kind = "exclusion",pattern_type = "regex", pattern = "^[O|S]")
  2. #> variable missing complete percent_complete percent_missing
  3. #> 1 Day 0 153 100 0
  4. #> 2 Month 0 153 100 0
  5. #> 3 Temp 0 153 100 0
  6. #> 4 Wind 0 153 100 0

To get this summary by group:

  1. test2 <- data.frame(ID= c("A","A","B","A","B"), Vals = c(rep(NA,4),"No"),ID2 = c("E","E","D","E","D"))
  2. na_summary(test2,grouping_cols = c("ID","ID2"))
  3. #> # A tibble: 2 x 7
  4. #> ID ID2 variable missing complete percent_complete percent_missing
  5. #> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
  6. #> 1 B D Vals 1 1 50 50
  7. #> 2 A E Vals 3 0 0 100
  1. na_summary(test2, grouping_cols="ID")
  2. #> Warning in na_summary.data.frame(test2, grouping_cols = "ID"): All non grouping
  3. #> values used. Using select non groups is currently not supported
  4. #> # A tibble: 4 x 6
  5. #> ID variable missing complete percent_complete percent_missing
  6. #> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
  7. #> 1 A Vals 3 0 0 100
  8. #> 2 A ID2 0 3 100 0
  9. #> 3 B Vals 1 1 50 50
  10. #> 4 B ID2 0 2 100 0
  • get_na_counts

This provides a convenient way to show the number of missing values
column-wise. It is relatively fast(tests done on about 400,000 rows,
took a few microseconds.)

To get the number of missing values in each column of airquality, we
can use the function as follows:

  1. get_na_counts(airquality)
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 37 7 0 0 0 0

The above might be less useful if one would like to get the results by
group. In that case, one can provide a grouping vector of names in
grouping_cols.

  1. test <- structure(list(Subject = structure(c(1L, 1L, 2L, 2L), .Label = c("A",
  2. "B"), class = "factor"), res = c(NA, 1, 2, 3), ID = structure(c(1L,
  3. 1L, 2L, 2L), .Label = c("1", "2"), class = "factor")), class = "data.frame", row.names = c(NA,
  4. -4L))
  5. get_na_counts(test, grouping_cols = "ID")
  6. #> # A tibble: 2 x 3
  7. #> ID Subject res
  8. #> <fct> <int> <int>
  9. #> 1 1 0 1
  10. #> 2 2 0 0
  • percent_missing

This is a very simple to use but quick way to take a look at the
percentage of data that is missing column-wise.

  1. percent_missing(airquality)
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 24.18301 4.575163 0 0 0 0

We can get the results by group by providing an optional grouping_cols
character vector.

  1. percent_missing(test, grouping_cols = "Subject")
  2. #> # A tibble: 2 x 3
  3. #> Subject res ID
  4. #> <fct> <dbl> <dbl>
  5. #> 1 A 50 0
  6. #> 2 B 0 0

To exclude some columns from the above exploration, one can provide an
optional character vector in exclude_cols.

  1. percent_missing(airquality,exclude_cols = c("Day","Temp"))
  2. #> Ozone Solar.R Wind Month
  3. #> 1 24.18301 4.575163 0 0
  • sort_by_missingness

This provides a very simple but relatively fast way to sort variables by
missingness. Unless otherwise stated, this does not currently support
arranging grouped percents.

Usage:

  1. sort_by_missingness(airquality, sort_by = "counts")
  2. #> variable percent
  3. #> 1 Wind 0
  4. #> 2 Temp 0
  5. #> 3 Month 0
  6. #> 4 Day 0
  7. #> 5 Solar.R 7
  8. #> 6 Ozone 37

To sort in descending order:

  1. sort_by_missingness(airquality, sort_by = "counts", descend = TRUE)
  2. #> variable percent
  3. #> 1 Ozone 37
  4. #> 2 Solar.R 7
  5. #> 3 Wind 0
  6. #> 4 Temp 0
  7. #> 5 Month 0
  8. #> 6 Day 0

To use percentages instead:

  1. sort_by_missingness(airquality, sort_by = "percents")
  2. #> variable percent
  3. #> 1 Wind 0.000000
  4. #> 2 Temp 0.000000
  5. #> 3 Month 0.000000
  6. #> 4 Day 0.000000
  7. #> 5 Solar.R 4.575163
  8. #> 6 Ozone 24.183007

Recoding as NA

  • recode_as_na

As the name might imply, this converts any value or vector of values to
NA i.e. we take a value such as “missing” or “NA” (not a real NA
according to R) and convert it to R’s known handler for missing values
(NA).

To use the function out of the box (with default arguments), one simply
does something like:

  1. dummy_test <- data.frame(ID = c("A","B","B","A"),
  2. values = c("n/a",NA,"Yes","No"))
  3. # Convert n/a and no to NA
  4. head(recode_as_na(dummy_test, value = c("n/a","No")))
  5. #> ID values
  6. #> 1 A <NA>
  7. #> 2 B <NA>
  8. #> 3 B Yes
  9. #> 4 A <NA>

Great, but I want to do so for specific columns not the entire dataset.
You can do this by providing column names to subset_cols.

  1. another_dummy <- data.frame(ID = 1:5, Subject = 7:11,
  2. Change = c("missing","n/a",2:4 ))
  3. # Only change values at the column Change
  4. head(recode_as_na(another_dummy, subset_cols = "Change", value = c("n/a","missing")))
  5. #> ID Subject Change
  6. #> 1 1 7 <NA>
  7. #> 2 2 8 <NA>
  8. #> 3 3 9 2
  9. #> 4 4 10 3
  10. #> 5 5 11 4

To recode columns using
RegEx,one can
provide pattern_type and a target pattern. Currently supported
pattern_types are starts_with, ends_with, contains and regex.
See docs for more details.:

  1. # only change at columns that start with Solar
  2. head(recode_as_na(airquality,value=190,pattern_type="starts_with",pattern="Solar"))
  3. #> Ozone Solar.R Wind Temp Month Day
  4. #> 1 41 NA 7.4 67 5 1
  5. #> 2 36 118 8.0 72 5 2
  6. #> 3 12 149 12.6 74 5 3
  7. #> 4 18 313 11.5 62 5 4
  8. #> 5 NA NA 14.3 56 5 5
  9. #> 6 28 NA 14.9 66 5 6
  1. # recode at columns that start with O or S(case sensitive)
  2. head(recode_as_na(airquality,value=c(67,118),pattern_type="starts_with",pattern="S|O"))
  3. #> Ozone Solar.R Wind Temp Month Day
  4. #> 1 41 190 7.4 67 5 1
  5. #> 2 36 NA 8.0 72 5 2
  6. #> 3 12 149 12.6 74 5 3
  7. #> 4 18 313 11.5 62 5 4
  8. #> 5 NA NA 14.3 56 5 5
  9. #> 6 28 NA 14.9 66 5 6
  1. # use my own RegEx
  2. head(recode_as_na(airquality,value=c(67,118),pattern_type="regex",pattern="(?i)^(s|o)"))
  3. #> Ozone Solar.R Wind Temp Month Day
  4. #> 1 41 190 7.4 67 5 1
  5. #> 2 36 NA 8.0 72 5 2
  6. #> 3 12 149 12.6 74 5 3
  7. #> 4 18 313 11.5 62 5 4
  8. #> 5 NA NA 14.3 56 5 5
  9. #> 6 28 NA 14.9 66 5 6
  • recode_as_na_if

This function allows one to deliberately introduce missing values if a
column meets a certain threshold of missing values. This is similar to
amputation but is much more basic. It is only provided here because it
is hoped it may be useful to someone for whatever reason.

  1. head(recode_as_na_if(airquality,sign="gt", percent_na=20))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 NA 190 7.4 67 5 1
  4. #> 2 NA 118 8.0 72 5 2
  5. #> 3 NA 149 12.6 74 5 3
  6. #> 4 NA 313 11.5 62 5 4
  7. #> 5 NA NA 14.3 56 5 5
  8. #> 6 NA NA 14.9 66 5 6
  • recode_as_na_str

This allows recoding as NA based on a string match.

  1. partial_match <- data.frame(A=c("Hi","match_me","nope"), B=c(NA, "not_me","nah"))
  2. recode_as_na_str(partial_match,"ends_with","ME", case_sensitive=FALSE)
  3. #> A B
  4. #> 1 Hi <NA>
  5. #> 2 <NA> <NA>
  6. #> 3 nope nah
  • recode_as_na_for

For all values greater/less/less or equal/greater or equal than some
value, can I convert them to NA?!

Yes You Can! All we have to do is use recode_as_na_for:

  1. head(recode_as_na_for(airquality,criteria="gt",value=25))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 NA NA 7.4 NA 5 1
  4. #> 2 NA NA 8.0 NA 5 2
  5. #> 3 12 NA 12.6 NA 5 3
  6. #> 4 18 NA 11.5 NA 5 4
  7. #> 5 NA NA 14.3 NA 5 5
  8. #> 6 NA NA 14.9 NA 5 6

To do so at specific columns, pass an optional subset_cols character
vector:

  1. head(recode_as_na_for(airquality, value=40,subset_cols=c("Solar.R","Ozone"), criteria="gt"))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 NA NA 7.4 67 5 1
  4. #> 2 36 NA 8.0 72 5 2
  5. #> 3 12 NA 12.6 74 5 3
  6. #> 4 18 NA 11.5 62 5 4
  7. #> 5 NA NA 14.3 56 5 5
  8. #> 6 28 NA 14.9 66 5 6

Recoding NA as

  • recode_na_as

Sometimes, for whatever reason, one would like to replace NAs with
whatever value they would like. recode_na_as provides a very simple
way to do just that.

  1. head(recode_na_as(airquality))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 41 190 7.4 67 5 1
  4. #> 2 36 118 8.0 72 5 2
  5. #> 3 12 149 12.6 74 5 3
  6. #> 4 18 313 11.5 62 5 4
  7. #> 5 0 0 14.3 56 5 5
  8. #> 6 28 0 14.9 66 5 6
  9. # use NaN
  10. head(recode_na_as(airquality, value=NaN))
  11. #> Ozone Solar.R Wind Temp Month Day
  12. #> 1 41 190 7.4 67 5 1
  13. #> 2 36 118 8.0 72 5 2
  14. #> 3 12 149 12.6 74 5 3
  15. #> 4 18 313 11.5 62 5 4
  16. #> 5 NaN NaN 14.3 56 5 5
  17. #> 6 28 NaN 14.9 66 5 6

As a “bonus”, you can manipulate the data only at specific columns as
shown here:

  1. head(recode_na_as(airquality, value=0, subset_cols="Ozone"))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 41 190 7.4 67 5 1
  4. #> 2 36 118 8.0 72 5 2
  5. #> 3 12 149 12.6 74 5 3
  6. #> 4 18 313 11.5 62 5 4
  7. #> 5 0 NA 14.3 56 5 5
  8. #> 6 28 NA 14.9 66 5 6

The above also supports custom recoding similar to recode_na_as:

  1. head(mde::recode_na_as(airquality, value=0, pattern_type="starts_with",pattern="Solar"))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 41 190 7.4 67 5 1
  4. #> 2 36 118 8.0 72 5 2
  5. #> 3 12 149 12.6 74 5 3
  6. #> 4 18 313 11.5 62 5 4
  7. #> 5 NA 0 14.3 56 5 5
  8. #> 6 28 0 14.9 66 5 6
  • column_based_recode

Ever needed to change values in a given column based on the proportions
of NAs in other columns(row-wise)?!. The goal of column_based_recode
is to achieve just that. Let’s see how we could do this with a simple
example:

  1. head(column_based_recode(airquality, values_from = "Wind", values_to="Wind", pattern_type = "regex", pattern = "Solar|Ozone"))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 41 190 7.4 67 5 1
  4. #> 2 36 118 8.0 72 5 2
  5. #> 3 12 149 12.6 74 5 3
  6. #> 4 18 313 11.5 62 5 4
  7. #> 5 NA NA 0.0 56 5 5
  8. #> 6 28 NA 14.9 66 5 6
  • custom_na_recode

This allows recoding NA values with common stats functions such as
mean,max,min,sd.

To use default values:

  1. head(custom_na_recode(airquality))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 41.00000 190.0000 7.4 67 5 1
  4. #> 2 36.00000 118.0000 8.0 72 5 2
  5. #> 3 12.00000 149.0000 12.6 74 5 3
  6. #> 4 18.00000 313.0000 11.5 62 5 4
  7. #> 5 42.12931 185.9315 14.3 56 5 5
  8. #> 6 28.00000 185.9315 14.9 66 5 6

To use select columns:

  1. head(custom_na_recode(airquality,func="mean",across_columns=c("Solar.R","Ozone")))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 41.00000 190.0000 7.4 67 5 1
  4. #> 2 36.00000 118.0000 8.0 72 5 2
  5. #> 3 12.00000 149.0000 12.6 74 5 3
  6. #> 4 18.00000 313.0000 11.5 62 5 4
  7. #> 5 42.12931 185.9315 14.3 56 5 5
  8. #> 6 28.00000 185.9315 14.9 66 5 6

To use a function from another package to perform replacements:

To perform a forward fill with dplyr’s lead:

  1. # use lag for a backfill
  2. head(custom_na_recode(airquality,func=dplyr::lead ))
  3. #> Ozone Solar.R Wind Temp Month Day
  4. #> 1 41 190 7.4 67 5 1
  5. #> 2 36 118 8.0 72 5 2
  6. #> 3 12 149 12.6 74 5 3
  7. #> 4 18 313 11.5 62 5 4
  8. #> 5 23 99 14.3 56 5 5
  9. #> 6 28 19 14.9 66 5 6

To perform replacement by group:

  1. some_data <- data.frame(ID=c("A1","A1","A1","A2","A2", "A2"),A=c(5,NA,0,8,3,4),B=c(10,0,0,NA,5,6),C=c(1,NA,NA,25,7,8))
  2. head(custom_na_recode(some_data,func = "mean", grouping_cols = "ID"))
  3. #> # A tibble: 6 x 4
  4. #> ID A B C
  5. #> <chr> <dbl> <dbl> <dbl>
  6. #> 1 A1 5 10 1
  7. #> 2 A1 2.5 0 1
  8. #> 3 A1 0 0 1
  9. #> 4 A2 8 5.5 25
  10. #> 5 A2 3 5 7
  11. #> 6 A2 4 6 8

Across specific columns:

  1. head(custom_na_recode(some_data,func = "mean", grouping_cols = "ID", across_columns = c("C", "A")))
  2. #> # A tibble: 6 x 4
  3. #> ID A B C
  4. #> <chr> <dbl> <dbl> <dbl>
  5. #> 1 A1 5 10 1
  6. #> 2 A1 2.5 0 1
  7. #> 3 A1 0 0 1
  8. #> 4 A2 8 NA 25
  9. #> 5 A2 3 5 7
  10. #> 6 A2 4 6 8
  • recode_na_if

Given a data.frame object, one can recode NAs as another value based
on a grouping variable. In the example below, we replace all NAs in
all columns with 0s if the ID is A2 or A3

  1. some_data <- data.frame(ID=c("A1","A2","A3", "A4"),
  2. A=c(5,NA,0,8), B=c(10,0,0,1),
  3. C=c(1,NA,NA,25))
  4. head(recode_na_if(some_data,grouping_col="ID", target_groups=c("A2","A3"),
  5. replacement= 0))
  6. #> # A tibble: 4 x 4
  7. #> ID A B C
  8. #> <chr> <dbl> <dbl> <dbl>
  9. #> 1 A1 5 10 1
  10. #> 2 A2 0 0 0
  11. #> 3 A3 0 0 0
  12. #> 4 A4 8 1 25

Dropping NAs

  • drop_na_if

Suppose you wanted to drop any column that has a percentage of NAs
greater than or equal to a certain value? drop_na_if does just that.

We can drop any columns that have greater than or equal(gteq) to 24% of
the values missing from airquality:

  1. head(drop_na_if(airquality, sign="gteq",percent_na = 24))
  2. #> Solar.R Wind Temp Month Day
  3. #> 1 190 7.4 67 5 1
  4. #> 2 118 8.0 72 5 2
  5. #> 3 149 12.6 74 5 3
  6. #> 4 313 11.5 62 5 4
  7. #> 5 NA 14.3 56 5 5
  8. #> 6 NA 14.9 66 5 6

The above also supports less than or equal to(lteq), equal to(eq),
greater than(gt) and less than(lt).

To keep certain columns despite fitting the target percent_na
criteria, one can provide an optional keep_columns character vector.

  1. head(drop_na_if(airquality, percent_na = 24, keep_columns = "Ozone"))
  2. #> Ozone Solar.R Wind Temp Month Day
  3. #> 1 41 190 7.4 67 5 1
  4. #> 2 36 118 8.0 72 5 2
  5. #> 3 12 149 12.6 74 5 3
  6. #> 4 18 313 11.5 62 5 4
  7. #> 5 NA NA 14.3 56 5 5
  8. #> 6 28 NA 14.9 66 5 6

Compare the above result to the following:

  1. head(drop_na_if(airquality, percent_na = 24))
  2. #> Solar.R Wind Temp Month Day
  3. #> 1 190 7.4 67 5 1
  4. #> 2 118 8.0 72 5 2
  5. #> 3 149 12.6 74 5 3
  6. #> 4 313 11.5 62 5 4
  7. #> 5 NA 14.3 56 5 5
  8. #> 6 NA 14.9 66 5 6

To drop groups that meet a set missingness criterion, we proceed as
follows.

  1. grouped_drop <- structure(list(ID = c("A", "A", "B", "A", "B"),
  2. Vals = c(4, NA, NA, NA, NA), Values = c(5, 6, 7, 8, NA)),
  3. row.names = c(NA, -5L), class = "data.frame")
  4. # Drop all columns for groups that meet a percent missingness of greater than or
  5. # equal to 67
  6. drop_na_if(grouped_drop,percent_na = 67,sign="gteq",
  7. grouping_cols = "ID")
  8. #> # A tibble: 3 x 3
  9. #> ID Vals Values
  10. #> <chr> <dbl> <dbl>
  11. #> 1 A 4 5
  12. #> 2 A NA 6
  13. #> 3 A NA 8
  • drop_row_if

This is similar to drop_na_if but does operations rowwise not
columnwise. Compare to the example above:

  1. # Drop rows with at least two NAs
  2. head(drop_row_if(airquality, sign="gteq", type="count" , value = 2))
  3. #> Dropped 2 rows.
  4. #> Ozone Solar.R Wind Temp Month Day
  5. #> 1 41 190 7.4 67 5 1
  6. #> 2 36 118 8.0 72 5 2
  7. #> 3 12 149 12.6 74 5 3
  8. #> 4 18 313 11.5 62 5 4
  9. #> 6 28 NA 14.9 66 5 6
  10. #> 7 23 299 8.6 65 5 7

To drop based on percentages:

  1. # Drops 42 rows
  2. head(drop_row_if(airquality, type="percent", value=16, sign="gteq",
  3. as_percent=TRUE))
  4. #> Dropped 42 rows.
  5. #> Ozone Solar.R Wind Temp Month Day
  6. #> 1 41 190 7.4 67 5 1
  7. #> 2 36 118 8.0 72 5 2
  8. #> 3 12 149 12.6 74 5 3
  9. #> 4 18 313 11.5 62 5 4
  10. #> 7 23 299 8.6 65 5 7
  11. #> 8 19 99 13.8 59 5 8

For more details, please see the documentation of drop_row_if.

  • drop_na_at

This provides a simple way to drop missing values only at specific
columns. It currently only returns those columns with their missing
values removed. See usage below. Further details are given in the
documentation. It is currently case sensitive.

  1. head(drop_na_at(airquality,pattern_type = "starts_with","O"))
  2. #> Ozone
  3. #> 1 41
  4. #> 2 36
  5. #> 3 12
  6. #> 4 18
  7. #> 5 28
  8. #> 6 23
  • drop_all_na

This drops columns where all values are missing.

  1. test2 <- data.frame(ID= c("A","A","B","A","B"), Vals = c(4,rep(NA, 4)))
  2. drop_all_na(test2, grouping_cols="ID")
  3. #> # A tibble: 3 x 2
  4. #> ID Vals
  5. #> <chr> <dbl>
  6. #> 1 A 4
  7. #> 2 A NA
  8. #> 3 A NA

Alternatively, we can drop groups where all variables are all NA.

  1. test2 <- data.frame(ID= c("A","A","B","A","B"), Vals = rep(NA, 5))
  2. head(drop_all_na(test, grouping_cols = "ID"))
  3. #> # A tibble: 4 x 3
  4. #> Subject res ID
  5. #> <fct> <dbl> <fct>
  6. #> 1 A NA 1
  7. #> 2 A 1 1
  8. #> 3 B 2 2
  9. #> 4 B 3 2
  • dict_recode

If one would like to recode column values using a “dictionary”,
dict_recode provides a simple way to do that. For example, if one
would like to convert NA values in Solar.R to 520 and those in
Ozone to 42, one simply calls the following:

  1. head(dict_recode(airquality, use_func="recode_na_as",
  2. patterns = c("solar", "ozone"),
  3. pattern_type="starts_with", values = c(520,42)))
  4. #> Ozone Solar.R Wind Temp Month Day
  5. #> 1 41 190 7.4 67 5 1
  6. #> 2 36 118 8.0 72 5 2
  7. #> 3 12 149 12.6 74 5 3
  8. #> 4 18 313 11.5 62 5 4
  9. #> 5 42 520 14.3 56 5 5
  10. #> 6 28 520 14.9 66 5 6

Please note that the mde project is released with a Contributor Code
of
Conduct
.
By contributing to this project, you agree to abide by its terms.

For further exploration, please browseVignettes("mde").

To raise an issue, please do so
here

Thank you, feedback is always welcome :)