naniar icon indicating copy to clipboard operation
naniar copied to clipboard

replace_with_na_where doesn't like anonymous functions

Open njtierney opened this issue 7 years ago • 3 comments

Using a function in replace_with_na_where doesn't really work.

library(naniar)

is_zero <- function(x) x == 0
replace_with_na_where(mtcars, cyl = cyl == 6, vs = is_zero)
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'closure'
#> Error in x[is.na(x)] <- TRUE: object of type 'closure' is not subsettable

From what I've seen going through the code, this is because replace_with_na_where doesn't evaluate the function when you use eval_tidy. I'm not sure how to fix that, but perhaps @lionel- might have some insight?

njtierney avatar Jan 17 '18 07:01 njtierney

Could you point me to some code or to a reprex? When you evaluate a function you get that function, only symbols and calls (i.e. symbolic objects) evaluate to something else then themselves.

lionel- avatar Jan 17 '18 08:01 lionel-

Hi @lionel- , sorry, the code is in this branch - https://github.com/njtierney/naniar/tree/scoped-replace-to-na, and the replace_with_na_where function lives in the scoped-replace-to-na and the reprex above should provide that error, I believe.

njtierney avatar Jan 17 '18 23:01 njtierney

If you want to call a function you have to somehow create a function call.

lionel- avatar Jan 18 '18 01:01 lionel-

I think this function is best covered now by case_when

library(naniar)
library(tidyverse)

mtcars <- as_tibble(mtcars)
mtcars
#> # A tibble: 32 × 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows

is_zero <- function(x) x == 0

mtcars %>% 
  mutate(
    cyl = case_when(
      cyl == 6 ~ NA,
      .default = cyl
    ),
    vs = case_when(
      is_zero(vs) ~ NA,
      .default = vs
    )
  )
#> # A tibble: 32 × 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21      NA  160    110  3.9   2.62  16.5    NA     1     4     4
#>  2  21      NA  160    110  3.9   2.88  17.0    NA     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4    NA  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0    NA     0     3     2
#>  6  18.1    NA  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8    NA     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2    NA  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows

Created on 2023-04-24 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.3 (2023-03-15)
#>  os       macOS Ventura 13.2
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Australia/Hobart
#>  date     2023-04-24
#>  pandoc   2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  cli           3.6.1      2023-03-23 [1] CRAN (R 4.2.0)
#>  colorspace    2.1-0      2023-01-23 [1] CRAN (R 4.2.0)
#>  digest        0.6.31     2022-12-11 [1] CRAN (R 4.2.0)
#>  dplyr       * 1.1.1      2023-03-22 [1] CRAN (R 4.2.0)
#>  evaluate      0.20       2023-01-17 [1] CRAN (R 4.2.0)
#>  fansi         1.0.4      2023-01-22 [1] CRAN (R 4.2.0)
#>  fastmap       1.1.1      2023-02-24 [1] CRAN (R 4.2.0)
#>  forcats     * 1.0.0      2023-01-29 [1] CRAN (R 4.2.0)
#>  fs            1.6.1      2023-02-06 [1] CRAN (R 4.2.0)
#>  generics      0.1.3      2022-07-05 [1] CRAN (R 4.2.0)
#>  ggplot2     * 3.4.2      2023-04-03 [1] CRAN (R 4.2.0)
#>  glue          1.6.2      2022-02-24 [1] CRAN (R 4.2.0)
#>  gtable        0.3.3      2023-03-21 [1] CRAN (R 4.2.0)
#>  hms           1.1.3      2023-03-21 [1] CRAN (R 4.2.0)
#>  htmltools     0.5.5      2023-03-23 [1] CRAN (R 4.2.0)
#>  knitr         1.42       2023-01-25 [1] CRAN (R 4.2.0)
#>  lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.2.0)
#>  lubridate   * 1.9.2      2023-02-10 [1] CRAN (R 4.2.0)
#>  magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.2.0)
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 4.2.0)
#>  naniar      * 1.0.0.9000 2023-04-10 [1] local
#>  pillar        1.9.0      2023-03-22 [1] CRAN (R 4.2.0)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.2.0)
#>  purrr       * 1.0.1      2023-01-10 [1] CRAN (R 4.2.0)
#>  R.cache       0.16.0     2022-07-21 [1] CRAN (R 4.2.0)
#>  R.methodsS3   1.8.2      2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0     2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.2     2022-11-11 [1] CRAN (R 4.2.0)
#>  R6            2.5.1      2021-08-19 [1] CRAN (R 4.2.0)
#>  readr       * 2.1.4      2023-02-10 [1] CRAN (R 4.2.0)
#>  reprex        2.0.2      2022-08-17 [1] CRAN (R 4.2.0)
#>  rlang         1.1.0      2023-03-14 [1] CRAN (R 4.2.0)
#>  rmarkdown     2.21       2023-03-26 [1] CRAN (R 4.2.0)
#>  rstudioapi    0.14       2022-08-22 [1] CRAN (R 4.2.0)
#>  scales        1.2.1      2022-08-20 [1] CRAN (R 4.2.0)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.2.0)
#>  stringi       1.7.12     2023-01-11 [1] CRAN (R 4.2.0)
#>  stringr     * 1.5.0      2022-12-02 [1] CRAN (R 4.2.0)
#>  styler        1.9.1      2023-03-04 [1] CRAN (R 4.2.0)
#>  tibble      * 3.2.1      2023-03-20 [1] CRAN (R 4.2.0)
#>  tidyr       * 1.3.0      2023-01-24 [1] CRAN (R 4.2.0)
#>  tidyselect    1.2.0      2022-10-10 [1] CRAN (R 4.2.0)
#>  tidyverse   * 2.0.0      2023-02-22 [1] CRAN (R 4.2.0)
#>  timechange    0.2.0      2023-01-11 [1] CRAN (R 4.2.0)
#>  tzdb          0.3.0      2022-03-28 [1] CRAN (R 4.2.0)
#>  utf8          1.2.3      2023-01-31 [1] CRAN (R 4.2.0)
#>  vctrs         0.6.2      2023-04-19 [1] CRAN (R 4.2.0)
#>  visdat        0.6.0      2023-02-02 [1] local
#>  withr         2.5.0      2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.39       2023-04-20 [1] CRAN (R 4.2.0)
#>  yaml          2.3.7      2023-01-23 [1] CRAN (R 4.2.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

njtierney avatar Apr 23 '23 22:04 njtierney