From 33efe90e5d268305166825788c6e9726d261f453 Mon Sep 17 00:00:00 2001 From: Benjamin Elbers Date: Tue, 6 Aug 2019 15:12:34 +0200 Subject: [PATCH] add support for tidyr::fill and tidyr::replace_na --- NAMESPACE | 2 ++ NEWS.md | 2 +- R/filter.R | 5 ----- R/mutate.R | 13 +++++++++++++ README.Rmd | 5 ++++- README.md | 7 ++++++- man/mutate.Rd | 6 ++++++ tests/testthat/test_mutate.R | 21 +++++++++++++++++++++ 8 files changed, 53 insertions(+), 8 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 7a0ffef..9521506 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(distinct_all) export(distinct_at) export(distinct_if) export(drop_na) +export(fill) export(filter) export(filter_all) export(filter_at) @@ -25,6 +26,7 @@ export(mutate) export(mutate_all) export(mutate_at) export(mutate_if) +export(replace_na) export(right_join) export(select) export(select_all) diff --git a/NEWS.md b/NEWS.md index 193f904..e31f18c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,5 @@ # 0.1.0.9000 -- added supoort for tidyr functions: gather, spread, drop_na (thanks @WilDoane and @jackhannah95) +- added supoort for tidyr functions: gather, spread (thanks @WilDoane), drop_na (@jackhannah95), fill and replace_na - use clisymbols for ellipsis - add number of remaining rows to filter (#23) - bugfix: do not report negative NA (#18) diff --git a/R/filter.R b/R/filter.R index 092749f..4317c43 100644 --- a/R/filter.R +++ b/R/filter.R @@ -10,11 +10,6 @@ #' #> filter: removed 18 rows (56%), 14 remaining #' filter(mtcars, mpg > 100) #' #> filter: removed all rows (100%) -#' -#' drop_na(airquality) -#' #> drop_na: removed 42 rows (27%), 111 rows remaining -#' drop_na(airquality, Ozone) -#' #> drop_na: removed 37 rows (24%), 116 rows remaining #' @import dplyr #' @import tidyr #' @export diff --git a/R/mutate.R b/R/mutate.R index 4d5aea5..b33a027 100644 --- a/R/mutate.R +++ b/R/mutate.R @@ -10,6 +10,7 @@ #' mutate(mtcars, new_var = NA) #> #> mutate: new variable 'new_var' with one unique value and 100% NA #' @import dplyr +#' @import tidyr #' @export mutate <- function(.data, ...) { log_mutate(.data, .fun = dplyr::mutate, .funname = "mutate", ...) @@ -79,6 +80,18 @@ add_count <- function(.data, ...) { log_mutate(.data, .fun = dplyr::add_count, .funname = "add_count", ...) } +#' @rdname mutate +#' @export +replace_na <- function(.data, ...) { + log_mutate(.data, .fun = tidyr::replace_na, .funname = "replace_na", ...) +} + +#' @rdname mutate +#' @export +fill <- function(.data, ...) { + log_mutate(.data, .fun = tidyr::fill, .funname = "fill", ...) +} + log_mutate <- function(.data, .fun, .funname, ...) { cols <- names(.data) newdata <- .fun(.data, ...) diff --git a/README.Rmd b/README.Rmd index d2e361e..2153523 100644 --- a/README.Rmd +++ b/README.Rmd @@ -81,7 +81,7 @@ i <- drop_na(airquality, Ozone) k <- drop_na(airquality, Wind, Temp, Month, Day) ``` -### mutate, transmute +### mutate, transmute, replace_na, fill ```{r} a <- mutate(mtcars, new_var = 1) @@ -94,6 +94,9 @@ g <- mutate(mtcars, am = ifelse(am == 1, NA, am)) h <- mutate(mtcars, am = recode(am, `0` = "zero", `1` = NA_character_)) i <- transmute(mtcars, mpg = mpg * 2, gear = gear + 1, new_var = vs + am) + +j <- replace_na(airquality, list(Solar.R = 1)) +k <- fill(airquality, Ozone) ``` ### select diff --git a/README.md b/README.md index af86182..94a109a 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ k <- drop_na(airquality, Wind, Temp, Month, Day) #> drop_na: no rows removed ``` -### mutate, transmute +### mutate, transmute, replace\_na, fill ``` r a <- mutate(mtcars, new_var = 1) @@ -122,6 +122,11 @@ i <- transmute(mtcars, mpg = mpg * 2, gear = gear + 1, new_var = vs + am) #> transmute: changed 32 values (100%) of 'mpg' (0 new NA) #> transmute: changed 32 values (100%) of 'gear' (0 new NA) #> transmute: new variable 'new_var' with 3 unique values and 0% NA + +j <- replace_na(airquality, list(Solar.R = 1)) +#> replace_na: converted 'Solar.R' from integer to double (7 fewer NA) +k <- fill(airquality, Ozone) +#> fill: changed 37 values (24%) of 'Ozone' (37 fewer NA) ``` ### select diff --git a/man/mutate.Rd b/man/mutate.Rd index 575679b..4df8e53 100644 --- a/man/mutate.Rd +++ b/man/mutate.Rd @@ -7,6 +7,8 @@ \alias{mutate_at} \alias{add_tally} \alias{add_count} +\alias{replace_na} +\alias{fill} \title{Wrapper around dplyr::mutate and related functions that prints information about the operation} \usage{ @@ -21,6 +23,10 @@ mutate_at(.data, ...) add_tally(.data, ...) add_count(.data, ...) + +replace_na(.data, ...) + +fill(.data, ...) } \arguments{ \item{.data}{a tbl; see \link[dplyr]{mutate}} diff --git a/tests/testthat/test_mutate.R b/tests/testthat/test_mutate.R index 5bf774d..da28816 100644 --- a/tests/testthat/test_mutate.R +++ b/tests/testthat/test_mutate.R @@ -180,3 +180,24 @@ test_that("mutate/transmute: partial matching", { f <- function() tidylog::transmute(mtcars, fun = 1) expect_message(f(), "new variable 'fun'") }) + +test_that("tidyr::replace_na", { + df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b")) + + expect_message({ + out <- tidylog::replace_na(df, list(x = 0, y = "unknown")) + }) + # with vector + expect_silent(dplyr::mutate(df, x = replace_na(x, 0))) + + expect_equal(tidyr::replace_na(df, list(x = 0, y = "unknown")), out) +}) + +test_that("tidyr::fill", { + df <- data.frame(Month = 1:12, Year = c(2000, rep(NA, 11))) + expect_message({ + out <- tidylog::fill(df, Year) + }) + + expect_equal(tidyr::fill(df, Year), out) +})