Closed jfdesomzee closed 1 year ago
I don't know if this is a bug - this errors in dplyr
as well:
library(dplyr, warn.conflicts = FALSE)
df <- tibble(x = integer(), y = character())
df %>%
dplyr::mutate(x = 1:n())
#> Error in `dplyr::mutate()`:
#> ℹ In argument: `x = 1:n()`.
#> Caused by error:
#> ! `x` must be size 0 or 1, not 2.
#> Backtrace:
#> ▆
#> 1. ├─df %>% dplyr::mutate(x = 1:n())
#> 2. ├─dplyr::mutate(., x = 1:n())
#> 3. ├─dplyr:::mutate.data.frame(., x = 1:n())
#> 4. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...))
#> 5. │ ├─base::withCallingHandlers(...)
#> 6. │ └─dplyr:::mutate_col(dots[[i]], .data, mask, new_columns)
#> 7. │ └─mask$eval_all_mutate(quo)
#> 8. │ └─dplyr (local) eval()
#> 9. ├─dplyr:::dplyr_internal_error(...)
#> 10. │ └─rlang::abort(class = c(class, "dplyr:::internal_error"), dplyr_error_data = data)
#> 11. │ └─rlang:::signal_abort(cnd, .file)
#> 12. │ └─base::signalCondition(cnd)
#> 13. └─dplyr (local) `<fn>`(`<dpl:::__>`)
#> 14. └─rlang::abort(message, class = error_class, parent = parent, call = error_call)
I'm going to close this - I don't think this is something that should be changed if it also fails in dplyr
.
If you have any other questions around it let me know.
I think the recommended way to deal with this would be to pass a vector of length 0 to mutate()
:
library(tidytable, warn.conflicts = FALSE)
#> As of tidytable v0.9.0 dotless versions of functions are exported.
#> You can now use `arrange()`/`mutate()`/etc. directly.
iris %>%
as_tidytable() %>%
filter(FALSE) %>%
mutate(Sepal.Length=integer())
#> # A tidytable: 0 × 5
#> # … with 5 variables: Sepal.Length <int>, Sepal.Width <dbl>,
#> # Petal.Length <dbl>, Petal.Width <dbl>, Species <fct>
Also FYI - you don't need to use require(magrittr)
if you're only using it for the %>%
pipe. tidytable
reexports %>%
.
this does not fail
library(dplyr, warn.conflicts = FALSE)
df <- tibble(x = integer(), y = character())
df %>%
dplyr::mutate(x = row_number())
#> # A tibble: 0 x 2
#> # ... with 2 variables: x <int>, y <chr>
Ah gotcha - 1:n()
fails because it's essentially calling 1:0
, which is a vector of length 2. Whereas dplyr's row_number()
is doing something different in the background.
In tidytable row_number()
does basically call 1:n()
.
All set.
pacman::p_load(tidytable)
df <- tidytable(x = integer(), y = character())
df %>%
mutate(x = row_number())
#> # A tidytable: 0 × 2
#> # … with 2 variables: x <int>, y <chr>
Hello,
I have an error when I create a variable with an existing name in a data table with 0 rows. Any idea how I could make this works?