Skip to content
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

More informative error for case_when() failing due to not matching NA #6206

Closed
marianschmidt opened this issue Mar 4, 2022 · 2 comments · Fixed by #6300
Closed

More informative error for case_when() failing due to not matching NA #6206

marianschmidt opened this issue Mar 4, 2022 · 2 comments · Fixed by #6300

Comments

@marianschmidt
Copy link

marianschmidt commented Mar 4, 2022

It seems that since recently mutate case_when() calls are more strictly evaluated with regard to the variable type (or it always was as strict and some other R or package update, now produces integer instead of double values).
That's fine, but I think, ideally the error message would be more informative than 'names' attribute [1] must be the same length as the vector [0], especially for this borderline case where (depending on the data) the same code might produce either integers or doubles.

library(dplyr)
#> 
#> Attache Paket: 'dplyr'
#> Die folgenden Objekte sind maskiert von 'package:stats':
#> 
#>     filter, lag
#> Die folgenden Objekte sind maskiert von 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(magrittr)

tibble(a = c("yes", "no"), 
       b = as.integer(c(0,2))) %>% 
  mutate(b = case_when(a == "yes" ~ NA_real_, 
                       TRUE ~ b))
#> Error in `mutate()`:
#> ! Problem while computing `b = case_when(a == "yes" ~ NA_real_, TRUE ~
#>   b)`.
#> Caused by error in `names(message) <- `*vtmp*``:
#> ! Attribut 'names' [1] muss dieselbe Länge haben wie der Vektor [0]

Created on 2022-03-04 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value
#>  version  R version 4.1.2 (2021-11-01)
#>  os       Windows 10 x64 (build 19044)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  German_Germany.1252
#>  ctype    German_Germany.1252
#>  tz       Europe/Berlin
#>  date     2022-03-04
#>  pandoc   2.14.0.3 @ C:/Program Files/RStudio/bin/pandoc/ (via rmarkdown)
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date (UTC) lib source
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.1)
#>  backports     1.4.1   2021-12-13 [1] CRAN (R 4.1.2)
#>  cli           3.2.0   2022-02-14 [1] CRAN (R 4.1.2)
#>  crayon        1.5.0   2022-02-14 [1] CRAN (R 4.1.2)
#>  DBI           1.1.2   2021-12-20 [1] CRAN (R 4.1.2)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.1.2)
#>  dplyr       * 1.0.8   2022-02-08 [1] CRAN (R 4.1.2)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.1)
#>  evaluate      0.15    2022-02-18 [1] CRAN (R 4.1.2)
#>  fansi         1.0.2   2022-01-14 [1] CRAN (R 4.1.2)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.1.1)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.1.2)
#>  generics      0.1.2   2022-01-31 [1] CRAN (R 4.1.2)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.1.2)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.1)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.1)
#>  knitr         1.37    2021-12-16 [1] CRAN (R 4.1.2)
#>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.2)
#>  magrittr    * 2.0.2   2022-01-26 [1] CRAN (R 4.1.2)
#>  pillar        1.7.0   2022-02-01 [1] CRAN (R 4.1.2)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.1)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.1)
#>  R.cache       0.15.0  2021-04-30 [1] CRAN (R 4.1.2)
#>  R.methodsS3   1.8.1   2020-08-26 [1] CRAN (R 4.1.1)
#>  R.oo          1.24.0  2020-08-26 [1] CRAN (R 4.1.1)
#>  R.utils       2.11.0  2021-09-26 [1] CRAN (R 4.1.2)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.1)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.1)
#>  rlang         1.0.1   2022-02-03 [1] CRAN (R 4.1.2)
#>  rmarkdown     2.12    2022-03-02 [1] CRAN (R 4.1.2)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.2)
#>  stringi       1.7.6   2021-11-29 [1] CRAN (R 4.1.2)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.1)
#>  styler        1.6.2   2021-09-23 [1] CRAN (R 4.1.2)
#>  tibble        3.1.6   2021-11-07 [1] CRAN (R 4.1.2)
#>  tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.1.2)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.1)
#>  vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.1)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.1.2)
#>  xfun          0.30    2022-03-02 [1] CRAN (R 4.1.2)
#>  yaml          2.3.5   2022-02-21 [1] CRAN (R 4.1.2)
#> 
#>  [1] C:/Users/ga27jar/Documents/R/win-library/4.1
#>  [2] C:/Program Files/R/R-4.1.2/library
#> 
#> ------------------------------------------------------------------------------
@marianschmidt marianschmidt changed the title More informative error for case_when failing due to not matching NA More informative error for case_when() failing due to not matching NA Mar 6, 2022
@DanChaltiel
Copy link
Contributor

Here is a related confusing error message with case_when() when the wrong NA type is used:

x=1:10
dplyr::case_when(
  x < 5 ~ 1,
  TRUE ~ NA
)
#> Error in names(message) <- `*vtmp*`: 'names' attribute [1] must be the same length as the vector [0]
dplyr::case_when(
  x < 5 ~ 1,
  TRUE ~ NA_real_
)
#>  [1]  1  1  1  1 NA NA NA NA NA NA

Created on 2022-04-04 by the reprex package (v2.0.1)

The error message is different but I's think the internal problem is the same. Not sure if it will be corrected by #6225 though.

@eutwt
Copy link
Contributor

eutwt commented Apr 4, 2022

Here's a reprex for the two examples above using dplyr installed from #6225

library(dplyr, warn.conflicts = FALSE)

tibble(a = c("yes", "no"), 
       b = as.integer(c(0,2))) %>% 
  mutate(b = case_when(a == "yes" ~ NA_real_, 
                       TRUE ~ b))
#> Error in `mutate()`:
#> ! Problem while computing `b = case_when(a == "yes" ~ NA_real_, TRUE ~
#>   b)`.
#> Caused by error in `case_when()`:
#> ! `b` must be a double vector, not an integer vector.

Created on 2022-04-04 by the reprex package (v2.0.1)

x=1:10
dplyr::case_when(
  x < 5 ~ 1,
  TRUE ~ NA
)
#> Error in `dplyr::case_when()`:
#> ! `NA` must be a double vector, not a logical vector.

Created on 2022-04-04 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants