Skip to content

Commit

Permalink
Merge pull request #813 from markfairbanks/filter-na-by
Browse files Browse the repository at this point in the history
Properly handle `NA` as `FALSE` when using `filter(.by = )`
  • Loading branch information
markfairbanks authored Jul 2, 2024
2 parents a1c5568 + 0ff6ed7 commit d1031a9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#### Bug fixes
* Attempting to rename columns using `group_by()` now leads to an error (#799)
* `pmap()` family works with data frame inputs (#803)
* `filter()` properly handles when comparing to `NA` when `.by` is used (#812)

# tidytable 0.11.0

Expand Down
2 changes: 2 additions & 0 deletions R/utils-general.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ call2_i_by <- function(.df, i, .by) {
j <- expr(.I[!!i])
dt_expr <- call2_j(.df, j, .by)
dt_expr <- call2("$", dt_expr, expr(V1))
# Properly handle NA equality, #812
dt_expr <- call2("replace_na", dt_expr, expr(FALSE), .ns = "tidytable")
dt_expr <- call2_i(.df, dt_expr)
dt_expr
}
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,14 @@ test_that("works on a grouped_tt", {
expect_equal(group_vars(res), "y")
expect_true(is_grouped_df(res))
})

test_that("properly handles NA comparisons when `.by` is used, #812", {
df <- data.table(x = c(1, NA), y = c("a", "b"))

res <- df %>%
filter(x == 1, .by = y)

expect_named(res, c("x", "y"))
expect_equal(res$x, 1)
expect_equal(res$y, "a")
})

0 comments on commit d1031a9

Please sign in to comment.