`parse_vector()`在R中的pivot文档中使用mutate_at时出现错误。

huangapple go评论60阅读模式
英文:

`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 &lt;- tribble(
  ~family,  ~dob_child1,  ~dob_child2, ~gender_child1, ~gender_child2,
  1L, &quot;1998-11-26&quot;, &quot;2000-01-29&quot;,             1L,             2L,
  2L, &quot;1996-06-22&quot;,           NA,             2L,             NA,
  3L, &quot;2002-07-11&quot;, &quot;2004-04-05&quot;,             2L,             2L,
  4L, &quot;2004-10-10&quot;, &quot;2009-08-27&quot;,             1L,             1L,
  5L, &quot;2000-12-05&quot;, &quot;2005-02-28&quot;,             2L,             1L,
)

family &lt;- family %&gt;% mutate_at(vars(starts_with(&quot;dob&quot;)), parse_date)  

error:

#Error in `mutate()`:
#i In argument: `dob_child1 = (function (x, format = &quot;&quot;, na = c(&quot;&quot;, &quot;NA&quot;),
  #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 &#39;family&#39; dataframe after you make changes to it. Also note that `mutate_at()` is deprecated and you can use `mutate(across())` in it&#39;s place, e.g.

``` r
library(tidyverse)

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

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

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

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

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

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

all.equal(output1, output2)
#&gt; [1] TRUE

<sup>Created on 2023-06-08 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月8日 11:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定