-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
rowwise()
size zero data frame edge case
#6303
Comments
I have a suspicion that this should result in chops of if (rowwise && vctrs::vec_is_list(column)) {
if (size == 0) {
SEXP ptype = PROTECT(Rf_getAttrib(column, Rf_install("ptype")));
column = PROTECT(Rf_allocVector(VECSXP, 1));
if (ptype != R_NilValue) {
SET_VECTOR_ELT(column, 0, ptype);
}
SET_PRCODE(prom, column);
UNPROTECT(2);
} else {
SET_PRCODE(prom, column);
}
} else {
SET_PRCODE(prom, Rf_lang3(dplyr::functions::vec_chop, column, rows));
} That would work ok in the previous example because Here is what it would look like on another example that uses list-of too: library(dplyr)
library(vctrs)
df <- tibble(x = list(c("abc", "de"), c("ab", "cd", "ef")))
df <- rowwise(df)
mutate(df, y = paste0(x, collapse = ","))
#> # A tibble: 2 × 2
#> # Rowwise:
#> x y
#> <list> <chr>
#> 1 <chr [2]> abc,de
#> 2 <chr [3]> ab,cd,ef
df0 <- df[0,]
# `NULL` works pretty well most of the time. `paste0(NULL) == character()`
mutate(df0, y = paste0(x, collapse = ","))
#> # A tibble: 0 × 2
#> # Rowwise:
#> # … with 2 variables: x <list>, y <chr>
# You can make it fail
mutate(df0, y = {stopifnot(is.character(x)); TRUE})
#> Error in `mutate()`:
#> ! Problem while computing `y = { ... }`.
#> Caused by error in `stopifnot()`:
#> ! is.character(x) is not TRUE
# Make it a list-of
df$x <- as_list_of(df$x)
df0 <- df[0,]
df0
#> # A tibble: 0 × 1
#> # Rowwise:
#> # … with 1 variable: x <list<chr>>
# Now it works
mutate(df0, y = {stopifnot(is.character(x)); TRUE})
#> # A tibble: 0 × 2
#> # Rowwise:
#> # … with 2 variables: x <list<chr>>, y <lgl> Created on 2022-06-19 by the reprex package (v2.0.1) |
There is a weird edge case that occurs with size zero data frames and
rowwise()
. In these cases, a list-col is made up of justlist()
, meaning there aren't any elements in there to extract and use in the expression.Practically, what ends up happening is that this
Rf_length(column) > 0
branch returnsFALSE
, so the list-column gets chopped, resulting inlist(list())
dplyr/src/chop.cpp
Lines 20 to 24 in 55dfc1c
That results in weird bugs, like this one:
I think that
Rf_length(column) > 0
was added to help with this case, but I think it may have made things more confusing and less consistent, because you aren't accessing the "elements" of the list column, you are working on the whole list-col itselfThe text was updated successfully, but these errors were encountered: