Problem while computing - variable must be size X or Y but not Z

Viewed 27

I am working on building a function in R that is an augment function, meaning it will add a column to a tibble. The basis for the function is it's corresponding vec function.

The issue I am running into and I am unsure of as to why is that I get the following exact error:

Error in `dplyr::mutate()`:
! Problem while computing `p = bootstrap_p_vec(.x = y)`.
✖ `p` must be size 25 or 1, not 32.
ℹ The error occurred in group 1: sim_number = 1.
Run `rlang::last_error()` to see where the error occurred.

Here is the trace:

> rlang::last_error()
<error/dplyr:::mutate_error>
Error in `dplyr::mutate()`:
! Problem while computing `p = bootstrap_p_vec(.x = y)`.
✖ `p` must be size 25 or 1, not 32.
ℹ The error occurred in group 1: sim_number = 1.
---
Backtrace:
 1. ... %>% bpa(.value = y)
 2. global bpa(., .value = y)
 5. dplyr:::mutate.data.frame(.data, !!!calls)
Run `rlang::last_trace()` to see the full context.
> rlang::last_trace()
<error/dplyr:::mutate_error>
Error in `dplyr::mutate()`:
! Problem while computing `p = bootstrap_p_vec(.x = y)`.
✖ `p` must be size 25 or 1, not 32.
ℹ The error occurred in group 1: sim_number = 1.
---
Backtrace:
     ▆
  1. ├─... %>% bpa(.value = y)
  2. ├─global bpa(., .value = y)
  3. │ ├─tibble::as_tibble(dplyr::mutate(.data, !!!calls))
  4. │ ├─dplyr::mutate(.data, !!!calls)
  5. │ └─dplyr:::mutate.data.frame(.data, !!!calls)
  6. │   └─dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
  7. │     ├─base::withCallingHandlers(...)
  8. │     └─mask$eval_all_mutate(quo)
  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(...)

Here are my functions: Vectorized

bootstrap_p_vec <- function(.x){
  
  x_term <- x
  
  if (!is.numeric(x)){
    rlang::abort(
      message = "'.x' must be a numeric vector",
      use_cli_format = TRUE
      )
  }
  
  e <- stats::ecdf(x_term)
  
  ret <- e(x_term)
  
  return(ret)
  
}

Augment Function

bpa <- function(.data, .value, .names = "auto"){
  
  column_expr <- rlang::enquo(.value)
  
  if(rlang::quo_is_missing(column_expr)){
    rlang::abort(
      message = "bootstrap_p_vec(.value) is missing",
      use_cli_format = TRUE
    )
  }
  
  col_nms <- names(tidyselect::eval_select(rlang::enquo(.value), .data))
  
  make_call <- function(col){
    rlang::call2(
      "bootstrap_p_vec",
      .x = rlang::sym(col),
      #.ns = "healthyR.ts"
    )
  }
  
  grid <- expand.grid(
    col = col_nms,
    stringsAsFactors = FALSE
  )
  
  calls <- purrr::pmap(.l = list(grid$col), make_call)
  
  if(any(.names == "auto")){
    newname <- "p"
  } else {
    newname <- as.list(.names)
  }
  
  calls <- purrr::set_names(calls, newname)
  
  ret <- tibble::as_tibble(dplyr::mutate(.data, !!!calls))
  
  return(ret)
}

Results of vec function come out right:

library(tidyverse)

x <- mtcars$mpg

> bootstrap_p_vec(x)
 [1] 0.62500 0.62500 0.78125 0.68750 0.46875 0.43750 0.12500 0.81250 0.78125
[10] 0.53125 0.40625 0.34375 0.37500 0.25000 0.06250 0.06250 0.15625 0.96875
[19] 0.93750 1.00000 0.71875 0.28125 0.25000 0.09375 0.53125 0.87500 0.84375
[28] 0.93750 0.31250 0.56250 0.18750 0.68750

In this instance x is 32 long but in the code I'm running y is only 25 so I'm not sure why 32 is coming into play.

I am using my functions tidy_bootstrap() and bootstrap_unnest_tbl with a proportion of 80% which gives back y a length of 25.

However, if I set the argument of .proportion to 1 in tidy_bootstrap() then the bpa function works.

UPDATE The call causing the error is the following:

tidy_bootstrap(x, .num_sims = 1) %>%
  bootstrap_unnest_tbl() %>%
  group_by(sim_number) %>%
  bpa(.value = y)
1 Answers

The bootstrap_p_vec() function mistakenly called x_term <- x and not x_term <- .x

Related