Skip to content

Commit

Permalink
add support for tidyr::fill and tidyr::replace_na
Browse files Browse the repository at this point in the history
  • Loading branch information
elbersb committed Aug 6, 2019
1 parent d4eff25 commit 33efe90
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 0 additions & 5 deletions R/filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions R/mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", ...)
Expand Down Expand Up @@ -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, ...)
Expand Down
5 changes: 4 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions man/mutate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/testthat/test_mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 33efe90

Please sign in to comment.