New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revisit dplyr::coalesce
with across
#54
Comments
This should work, but I can't immediately understand why it doesn't: library(dplyr, warn.conflicts = FALSE)
df <- tibble(
id = 1:5,
w = c(10, NA, NA, NA, 14),
x = c(NA, 21, 22, 23, NA),
y = c(NA, NA, 32, 33, NA),
z = c(NA, NA, NA, 43, 44)
)
df %>%
mutate(a = coalesce(!!!across(-id)))
#> Error in .subset2(chunks, self$get_current_group()): attempt to select less than one element in integerOneIndex Created on 2020-04-14 by the reprex package (v0.3.0) |
splicing happens "too early", but this works: library(dplyr, warn.conflicts = FALSE)
df <- tibble(
id = 1:5,
w = c(10, NA, NA, NA, 14),
x = c(NA, 21, 22, 23, NA),
y = c(NA, NA, 32, 33, NA),
z = c(NA, NA, NA, 43, 44)
)
coacross <- function(...) {
coalesce(!!!across(...))
}
df %>%
mutate(a = coacross(-id))
#> # A tibble: 5 x 6
#> id w x y z a
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 10 NA NA NA 10
#> 2 2 NA 21 NA NA 21
#> 3 3 NA 22 32 NA 22
#> 4 4 NA 23 33 43 23
#> 5 5 14 NA NA 44 14 Created on 2020-04-15 by the reprex package (v0.3.0) |
Feature request: coalesce working backwards, i.e. returning the last non-missing column: |
In case anyone comes across this issue after googling, another workaround is to use If you want to do it in reverse you could just library(dplyr, warn.conflicts = FALSE)
df <- tibble(
id = 1:5,
w = c(10, NA, NA, NA, 14),
x = c(NA, 21, 22, 23, NA),
y = c(NA, NA, 32, 33, NA),
z = c(NA, NA, NA, 43, 44)
)
df %>%
mutate(a = do.call(coalesce, across(-id)))
#> # A tibble: 5 × 6
#> id w x y z a
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 10 NA NA NA 10
#> 2 2 NA 21 NA NA 21
#> 3 3 NA 22 32 NA 22
#> 4 4 NA 23 33 43 23
#> 5 5 14 NA NA 44 14
df %>%
mutate(a = do.call(coalesce, rev(across(-id))))
#> # A tibble: 5 × 6
#> id w x y z a
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 10 NA NA NA 10
#> 2 2 NA 21 NA NA 21
#> 3 3 NA 22 32 NA 32
#> 4 4 NA 23 33 43 43
#> 5 5 14 NA NA 44 44 Created on 2021-08-04 by the reprex package (v2.0.0) |
What about:
|
Since we're revisiting The use case is data where missing or special values are encoded as We have |
Wailing, gnashing my teeth, rending my clothing in the streets because |
With dplyr 1.0.0 introducing
c_across
andacross
I was wondering if it was possible to revisit tidyverse/dplyr#3548, by allowingdplyr::coalesce
to work more naturally with the newacross
orc_across
functions.After reading the row-wise article, I expected
dplyr::coalesce
to work likerowSums
since it naturally works across rows, or at worst it would work likerowwise
=>sum
.However,
coalesce
doesn't seem to work with theacross
family at all, as you can see in the code below.Would it be possible to make
coalesce
compatible with the newacross
workflow?Created on 2020-04-14 by the reprex package (v0.3.0)
The text was updated successfully, but these errors were encountered: