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

replace_na lacks tidyselect specification #912

Closed
qiushiyan opened this issue Mar 14, 2020 · 4 comments · Fixed by #1219
Closed

replace_na lacks tidyselect specification #912

qiushiyan opened this issue Mar 14, 2020 · 4 comments · Fixed by #1219

Comments

@qiushiyan
Copy link

Currently repalce_na() only use a list specification to replace missing values rather than the tidyselect style Things could get a little bit annoying when you have many columns containing NA values and replace_na(df, across(everthing, 0)) does not work

library(tidyverse)
df <- tibble(
  x = c(NA, 1, 2),
  y = c("a", NA, NA),
)

df %>% replace_na(list(x = 0,y = "None"))
#> # A tibble: 3 x 2
#>       x y    
#>   <dbl> <chr>
#> 1     0 a    
#> 2     1 None 
#> 3     2 None

df %>% 
  replace_na(across(x, 0),
             across(y, "None"))
#> Error: peek_mask() must only be used inside dplyr verbs
#> Error: 1 components of `...` were not used.
#> 
#> We detected these problematic arguments:
#> * `..1`
#> 
#> Did you misspecify an argument?

Created on 2020-03-14 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> Warning in system("timedatectl", intern = TRUE): running command 'timedatectl'
#> had status 1
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.0 (2019-04-26)
#>  os       Ubuntu 16.04.6 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  C.UTF-8                     
#>  ctype    C.UTF-8                     
#>  tz       Etc/UTC                     
#>  date     2020-03-14                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version      date       lib source                           
#>  assertthat    0.2.1        2019-03-21 [1] RSPM (R 3.6.0)                   
#>  backports     1.1.5        2019-10-02 [1] RSPM (R 3.6.0)                   
#>  broom         0.5.4        2020-01-27 [1] RSPM (R 3.6.0)                   
#>  callr         3.4.1        2020-01-24 [1] RSPM (R 3.6.0)                   
#>  cellranger    1.1.0        2016-07-27 [1] RSPM (R 3.6.0)                   
#>  cli           2.0.2        2020-02-28 [1] RSPM (R 3.6.0)                   
#>  colorspace    1.4-1        2019-03-18 [1] RSPM (R 3.6.0)                   
#>  crayon        1.3.4        2017-09-16 [1] RSPM (R 3.6.0)                   
#>  DBI           1.1.0        2019-12-15 [1] RSPM (R 3.6.0)                   
#>  dbplyr        1.4.2        2019-06-17 [1] RSPM (R 3.6.0)                   
#>  desc          1.2.0        2018-05-01 [1] RSPM (R 3.6.0)                   
#>  devtools      2.2.1        2019-09-24 [1] RSPM (R 3.6.0)                   
#>  digest        0.6.25       2020-02-23 [1] RSPM (R 3.6.0)                   
#>  dplyr       * 0.8.99.9000  2020-03-10 [1] Github (tidyverse/dplyr@87ad0b3) 
#>  ellipsis      0.3.0        2019-09-20 [1] RSPM (R 3.6.0)                   
#>  evaluate      0.14         2019-05-28 [1] RSPM (R 3.6.0)                   
#>  fansi         0.4.1        2020-01-08 [1] RSPM (R 3.6.0)                   
#>  forcats     * 0.4.0        2019-02-17 [1] RSPM (R 3.6.0)                   
#>  fs            1.3.1        2019-05-06 [1] RSPM (R 3.6.0)                   
#>  generics      0.0.2        2018-11-29 [1] RSPM (R 3.6.0)                   
#>  ggplot2     * 3.2.1        2019-08-10 [1] RSPM (R 3.6.0)                   
#>  glue          1.3.1        2019-03-12 [1] RSPM (R 3.6.0)                   
#>  gtable        0.3.0        2019-03-25 [1] RSPM (R 3.6.0)                   
#>  haven         2.2.0        2019-11-08 [1] RSPM (R 3.6.0)                   
#>  highr         0.8          2019-03-20 [1] RSPM (R 3.6.0)                   
#>  hms           0.5.3        2020-01-08 [1] RSPM (R 3.6.0)                   
#>  htmltools     0.4.0        2019-10-04 [1] RSPM (R 3.6.0)                   
#>  httr          1.4.1        2019-08-05 [1] RSPM (R 3.6.0)                   
#>  jsonlite      1.6.1        2020-02-02 [1] RSPM (R 3.6.0)                   
#>  knitr         1.28         2020-02-06 [1] RSPM (R 3.6.0)                   
#>  lattice       0.20-38      2018-11-04 [2] CRAN (R 3.6.0)                   
#>  lazyeval      0.2.2        2019-03-15 [1] RSPM (R 3.6.0)                   
#>  lifecycle     0.2.0        2020-03-10 [1] Github (r-lib/lifecycle@1b13d96) 
#>  lubridate     1.7.4        2018-04-11 [1] RSPM (R 3.6.0)                   
#>  magrittr      1.5          2014-11-22 [1] RSPM (R 3.6.0)                   
#>  memoise       1.1.0        2017-04-21 [1] RSPM (R 3.6.0)                   
#>  modelr        0.1.5        2019-08-08 [1] RSPM (R 3.6.0)                   
#>  munsell       0.5.0        2018-06-12 [1] RSPM (R 3.6.0)                   
#>  nlme          3.1-139      2019-04-09 [2] CRAN (R 3.6.0)                   
#>  pillar        1.4.3.9000   2020-02-12 [1] Github (r-lib/pillar@8f5918c)    
#>  pkgbuild      1.0.6        2019-10-09 [1] RSPM (R 3.6.0)                   
#>  pkgconfig     2.0.3        2019-09-22 [1] RSPM (R 3.6.0)                   
#>  pkgload       1.0.2        2018-10-29 [1] RSPM (R 3.6.0)                   
#>  prettyunits   1.1.1        2020-01-24 [1] RSPM (R 3.6.0)                   
#>  processx      3.4.2        2020-02-09 [1] RSPM (R 3.6.0)                   
#>  ps            1.3.0        2018-12-21 [1] RSPM (R 3.6.0)                   
#>  purrr       * 0.3.3        2019-10-18 [1] RSPM (R 3.6.0)                   
#>  R6            2.4.1        2019-11-12 [1] RSPM (R 3.6.0)                   
#>  Rcpp          1.0.3        2019-11-08 [1] RSPM (R 3.6.0)                   
#>  readr       * 1.3.1        2018-12-21 [1] RSPM (R 3.6.0)                   
#>  readxl        1.3.1        2019-03-13 [1] RSPM (R 3.6.0)                   
#>  remotes       2.1.0        2019-06-24 [1] RSPM (R 3.6.0)                   
#>  reprex        0.3.0        2019-05-16 [1] RSPM (R 3.6.0)                   
#>  rlang         0.4.5        2020-03-10 [1] Github (r-lib/rlang@6592336)     
#>  rmarkdown     2.1          2020-01-20 [1] RSPM (R 3.6.0)                   
#>  rprojroot     1.3-2        2018-01-03 [1] RSPM (R 3.6.0)                   
#>  rvest         0.3.5        2019-11-08 [1] RSPM (R 3.6.0)                   
#>  scales        1.1.0        2019-11-18 [1] RSPM (R 3.6.0)                   
#>  sessioninfo   1.1.1        2018-11-05 [1] RSPM (R 3.6.0)                   
#>  stringi       1.4.5        2020-01-11 [1] RSPM (R 3.6.0)                   
#>  stringr     * 1.4.0        2019-02-10 [1] RSPM (R 3.6.0)                   
#>  testthat      2.3.1        2019-12-01 [1] RSPM (R 3.6.0)                   
#>  tibble      * 2.99.99.9014 2020-03-10 [1] Github (tidyverse/tibble@725e41e)
#>  tidyr       * 1.0.2        2020-01-24 [1] RSPM (R 3.6.0)                   
#>  tidyselect    1.0.0        2020-01-27 [1] RSPM (R 3.6.0)                   
#>  tidyverse   * 1.3.0        2019-11-21 [1] RSPM (R 3.6.0)                   
#>  usethis       1.5.1        2019-07-04 [1] RSPM (R 3.6.0)                   
#>  utf8          1.1.4        2018-05-24 [1] RSPM (R 3.6.0)                   
#>  vctrs         0.2.99.9010  2020-03-10 [1] Github (r-lib/vctrs@97c6fb4)     
#>  withr         2.1.2        2018-03-15 [1] RSPM (R 3.6.0)                   
#>  xfun          0.12         2020-01-13 [1] RSPM (R 3.6.0)                   
#>  xml2          1.2.2        2019-08-09 [1] RSPM (R 3.6.0)                   
#>  yaml          2.2.1        2020-02-01 [1] RSPM (R 3.6.0)                   
#> 
#> [1] /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6
#> [2] /opt/R/3.6.0/lib/R/library
@huftis
Copy link

huftis commented Mar 31, 2020

Also related: #843

@qiushiyan
Copy link
Author

I think this is the same problem with the fill argument in tidyr::complete()

@hadley
Copy link
Member

hadley commented Apr 1, 2020

I think it's more that replace_na() is superseded in favour of across() + coalesce():

library(dplyr, warn.conflicts = FALSE)
df <- tibble(
  x = c(NA, 1, 2),
  y = c("a", NA, NA),
)

df %>% mutate(
  across(is.numeric, coalesce, 0),
  across(is.character, coalesce, "")
)
#> # A tibble: 3 x 2
#>       x y    
#>   <dbl> <chr>
#> 1     0 "a"  
#> 2     1 ""   
#> 3     2 ""

Created on 2020-04-01 by the reprex package (v0.3.0)

@hadley hadley added documentation pivoting ♻️ pivot rectangular data to different "shapes" labels Apr 1, 2020
@hadley hadley added this to the v1.1.0 milestone Apr 21, 2020
@hadley
Copy link
Member

hadley commented Apr 22, 2020

Needs to wait until the next release of tidyr, since this release will be out before dplyr 1.0.0.

@hadley hadley removed this from the v1.1.0 milestone Apr 22, 2020
@hadley hadley added missing values 💀 and removed pivoting ♻️ pivot rectangular data to different "shapes" labels May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants