Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

`parse_vector()` error when using mutate_at in pivot vignette in R

I was reading the pivot vignette and there is a chunk that when I run resulted in an error. I would appreciate if someone could help me with this issue.

family <- tribble(
  ~family,  ~dob_child1,  ~dob_child2, ~gender_child1, ~gender_child2,
  1L, "1998-11-26", "2000-01-29",             1L,             2L,
  2L, "1996-06-22",           NA,             2L,             NA,
  3L, "2002-07-11", "2004-04-05",             2L,             2L,
  4L, "2004-10-10", "2009-08-27",             1L,             1L,
  5L, "2000-12-05", "2005-02-28",             2L,             1L,
)

family <- family %>% mutate_at(vars(starts_with("dob")), parse_date)  

error:

#Error in `mutate()`:
#i In argument: `dob_child1 = (function (x, format = "", na = c("", "NA"),
  #locale = default_locale(), ...`.
#Caused by error in `parse_vector()`:
#! is.character(x) is not TRUE

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I suspect the problem is caused by not renaming the ‘family’ dataframe after you make changes to it. Also note that mutate_at() is deprecated and you can use mutate(across()) in it’s place, e.g.

library(tidyverse)

family <- tribble(
  ~family,  ~dob_child1,  ~dob_child2, ~gender_child1, ~gender_child2,
  1L, "1998-11-26", "2000-01-29",             1L,             2L,
  2L, "1996-06-22",           NA,             2L,             NA,
  3L, "2002-07-11", "2004-04-05",             2L,             2L,
  4L, "2004-10-10", "2009-08-27",             1L,             1L,
  5L, "2000-12-05", "2005-02-28",             2L,             1L,
)

family <- family %>% mutate_at(vars(starts_with("dob")), parse_date)
family
#> # A tibble: 5 × 5
#>   family dob_child1 dob_child2 gender_child1 gender_child2
#>    <int> <date>     <date>             <int>         <int>
#> 1      1 1998-11-26 2000-01-29             1             2
#> 2      2 1996-06-22 NA                     2            NA
#> 3      3 2002-07-11 2004-04-05             2             2
#> 4      4 2004-10-10 2009-08-27             1             1
#> 5      5 2000-12-05 2005-02-28             2             1

# Run it again
family <- family %>% mutate_at(vars(starts_with("dob")), parse_date)
#> Error in `mutate()`:
#> ℹ In argument: `dob_child1 = (function (x, format = "", na = c("",
#>   "NA"), locale = default_locale(), ...`.
#> Caused by error in `parse_vector()`:
#> ! is.character(x) is not TRUE
#> Backtrace:
#>      ▆
#>   1. ├─family %>% mutate_at(vars(starts_with("dob")), parse_date)
#>   2. ├─dplyr::mutate_at(., vars(starts_with("dob")), parse_date)
#>   3. │ ├─dplyr::mutate(.tbl, !!!funs)
#>   4. │ └─dplyr:::mutate.data.frame(.tbl, !!!funs)
#>   5. │   └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#>   6. │     ├─base::withCallingHandlers(...)
#>   7. │     └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#>   8. │       └─mask$eval_all_mutate(quo)
#>   9. │         └─dplyr (local) eval()
#>  10. ├─readr (local) `<fn>`(dob_child1)
#>  11. │ └─readr::parse_vector(...)
#>  12. │   └─base::stopifnot(is.character(x))
#>  13. │     └─base::stop(simpleError(msg, call = if (p <- sys.parent(1L)) sys.call(p)))
#>  14. └─dplyr (local) `<fn>`(`<smplErrr>`)
#>  15.   └─rlang::abort(message, class = error_class, parent = parent, call = error_call)

## start from scratch ##
family <- tribble(
  ~family,  ~dob_child1,  ~dob_child2, ~gender_child1, ~gender_child2,
  1L, "1998-11-26", "2000-01-29",             1L,             2L,
  2L, "1996-06-22",           NA,             2L,             NA,
  3L, "2002-07-11", "2004-04-05",             2L,             2L,
  4L, "2004-10-10", "2009-08-27",             1L,             1L,
  5L, "2000-12-05", "2005-02-28",             2L,             1L,
)

output1 <- family %>% mutate_at(vars(starts_with("dob")), parse_date)
output1
#> # A tibble: 5 × 5
#>   family dob_child1 dob_child2 gender_child1 gender_child2
#>    <int> <date>     <date>             <int>         <int>
#> 1      1 1998-11-26 2000-01-29             1             2
#> 2      2 1996-06-22 NA                     2            NA
#> 3      3 2002-07-11 2004-04-05             2             2
#> 4      4 2004-10-10 2009-08-27             1             1
#> 5      5 2000-12-05 2005-02-28             2             1

# 'across' syntax
output2 <- family %>% mutate(across(starts_with("dob"), parse_date))
output2
#> # A tibble: 5 × 5
#>   family dob_child1 dob_child2 gender_child1 gender_child2
#>    <int> <date>     <date>             <int>         <int>
#> 1      1 1998-11-26 2000-01-29             1             2
#> 2      2 1996-06-22 NA                     2            NA
#> 3      3 2002-07-11 2004-04-05             2             2
#> 4      4 2004-10-10 2009-08-27             1             1
#> 5      5 2000-12-05 2005-02-28             2             1

all.equal(output1, output2)
#> [1] TRUE

Created on 2023-06-08 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading