Skip to content

Commit

Permalink
funs: add support for replace_na() when input is a vector
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed May 6, 2024
1 parent 8c85cae commit b577cab
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
- from package `lubridate`: `make_datetime()`.

- from package `stringr`: `str_dup()`, `str_split()`, `str_split_i()`.

- from package `tidyr`: `replace_na()` (the data.frame method was already
translated but not the vector one that can be used in `mutate()` for example).

## Bug fixes

Expand Down
5 changes: 5 additions & 0 deletions R/funs-default.R
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ pl_rank <- function(x, ...) {
x$rank()
}

pl_replace_na <- function(data, replace, ...) {
check_empty_dots(...)
data$fill_null(replace)
}

pl_round <- function(x, digits = 0, ...) {
check_empty_dots(...)
x$round(decimals = digits)
Expand Down
14 changes: 9 additions & 5 deletions tests/tinytest/test_replace_na.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
source("helpers.R")
using("tidypolars")

pl_test <- pl$DataFrame(x = c(NA, 1), y = c(2, NA))
pl_test <- polars::pl$DataFrame(x = c(NA, 1), y = c(2, NA))

expect_is_tidypolars(replace_na(pl_test, 0))

expect_equal(
replace_na(pl_test, 0) |>
as.data.frame(),
replace_na(pl_test, 0),
data.frame(x = c(0, 1), y = c(2, 0))
)

expect_equal(
replace_na(pl_test, list(x = 0, y = 999)) |>
as.data.frame(),
replace_na(pl_test, list(x = 0, y = 999)),
data.frame(x = c(0, 1), y = c(2, 999))
)

expect_equal(
pl_test |>
mutate(x = replace_na(x, 0)) ,
data.frame(x = c(0, 1), y = c(2, NA))
)
14 changes: 9 additions & 5 deletions tests/tinytest/test_replace_na_lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ Sys.setenv('TIDYPOLARS_TEST' = TRUE)
source("helpers.R")
using("tidypolars")

pl_test <- pl$LazyFrame(x = c(NA, 1), y = c(2, NA))
pl_test <- polars::pl$LazyFrame(x = c(NA, 1), y = c(2, NA))

expect_is_tidypolars(replace_na(pl_test, 0))

expect_equal_lazy(
replace_na(pl_test, 0) |>
as.data.frame(),
replace_na(pl_test, 0),
data.frame(x = c(0, 1), y = c(2, 0))
)

expect_equal_lazy(
replace_na(pl_test, list(x = 0, y = 999)) |>
as.data.frame(),
replace_na(pl_test, list(x = 0, y = 999)),
data.frame(x = c(0, 1), y = c(2, 999))
)

expect_equal_lazy(
pl_test |>
mutate(x = replace_na(x, 0)) ,
data.frame(x = c(0, 1), y = c(2, NA))
)

Sys.setenv('TIDYPOLARS_TEST' = FALSE)
1 change: 1 addition & 0 deletions vignettes/r-and-polars-expressions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ out <- tribble(
"`stringr`", "`str_to_title`",
"`stringr`", "`str_to_upper`",
"`stringr`", "`word`",
"`tidyr`", "`replace_na`",
"`tools`", "`toTitleCase`"
) |>
mutate(Notes = case_when(
Expand Down

0 comments on commit b577cab

Please sign in to comment.