英文:
`parse_vector()` error when using mutate_at in pivot vignette in R
问题
我在阅读"pivot vignette"时遇到了一个块,当我运行它时出现了错误。如果有人能帮助我解决这个问题,我将不胜感激。
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 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
英文:
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
答案1
得分: 2
我怀疑问题是因为在对'family'数据框进行更改后没有重新命名它。还请注意,mutate_at()
已被弃用,你可以使用 mutate(across())
来替代它,例如:
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
# 再次运行
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)
## 从头开始 ##
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' 语法
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, output
<details>
<summary>英文:</summary>
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.
``` r
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
<sup>Created on 2023-06-08 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论