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

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

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

问题

我在阅读"pivot vignette"时遇到了一个块,当我运行它时出现了错误。如果有人能帮助我解决这个问题,我将不胜感激。

  1. family <- tribble(
  2. ~family, ~dob_child1, ~dob_child2, ~gender_child1, ~gender_child2,
  3. 1L, "1998-11-26", "2000-01-29", 1L, 2L,
  4. 2L, "1996-06-22", NA, 2L, NA,
  5. 3L, "2002-07-11", "2004-04-05", 2L, 2L,
  6. 4L, "2004-10-10", "2009-08-27", 1L, 1L,
  7. 5L, "2000-12-05", "2005-02-28", 2L, 1L,
  8. )
  9. family <- family %>% mutate_at(vars(starts_with("dob")), parse_date)

错误信息:

  1. #Error in `mutate()`:
  2. #i In argument: `dob_child1 = (function (x, format = "", na = c("", "NA"),
  3. #locale = default_locale(), ...`.
  4. #Caused by error in `parse_vector()`:
  5. #! 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.

  1. family &lt;- tribble(
  2. ~family, ~dob_child1, ~dob_child2, ~gender_child1, ~gender_child2,
  3. 1L, &quot;1998-11-26&quot;, &quot;2000-01-29&quot;, 1L, 2L,
  4. 2L, &quot;1996-06-22&quot;, NA, 2L, NA,
  5. 3L, &quot;2002-07-11&quot;, &quot;2004-04-05&quot;, 2L, 2L,
  6. 4L, &quot;2004-10-10&quot;, &quot;2009-08-27&quot;, 1L, 1L,
  7. 5L, &quot;2000-12-05&quot;, &quot;2005-02-28&quot;, 2L, 1L,
  8. )
  9. family &lt;- family %&gt;% mutate_at(vars(starts_with(&quot;dob&quot;)), parse_date)

error:

  1. #Error in `mutate()`:
  2. #i In argument: `dob_child1 = (function (x, format = &quot;&quot;, na = c(&quot;&quot;, &quot;NA&quot;),
  3. #locale = default_locale(), ...`.
  4. #Caused by error in `parse_vector()`:
  5. #! is.character(x) is not TRUE

答案1

得分: 2

我怀疑问题是因为在对'family'数据框进行更改后没有重新命名它。还请注意,mutate_at() 已被弃用,你可以使用 mutate(across()) 来替代它,例如:

  1. library(tidyverse)
  2. family <- tribble(
  3. ~family, ~dob_child1, ~dob_child2, ~gender_child1, ~gender_child2,
  4. 1L, "1998-11-26", "2000-01-29", 1L, 2L,
  5. 2L, "1996-06-22", NA, 2L, NA,
  6. 3L, "2002-07-11", "2004-04-05", 2L, 2L,
  7. 4L, "2004-10-10", "2009-08-27", 1L, 1L,
  8. 5L, "2000-12-05", "2005-02-28", 2L, 1L,
  9. )
  10. family <- family %>% mutate_at(vars(starts_with("dob")), parse_date)
  11. family
  12. #> # A tibble: 5 × 5
  13. #> family dob_child1 dob_child2 gender_child1 gender_child2
  14. #> <int> <date> <date> <int> <int>
  15. #> 1 1 1998-11-26 2000-01-29 1 2
  16. #> 2 2 1996-06-22 NA 2 NA
  17. #> 3 3 2002-07-11 2004-04-05 2 2
  18. #> 4 4 2004-10-10 2009-08-27 1 1
  19. #> 5 5 2000-12-05 2005-02-28 2 1
  20. # 再次运行
  21. family <- family %>% mutate_at(vars(starts_with("dob")), parse_date)
  22. #> Error in `mutate()`:
  23. #> ℹ In argument: `dob_child1 = (function (x, format = "", na = c("",
  24. #> "NA"), locale = default_locale(), ...`.
  25. #> Caused by error in `parse_vector()`:
  26. #> ! is.character(x) is not TRUE
  27. #> Backtrace:
  28. #> ▆
  29. #> 1. ├─family %>% mutate_at(vars(starts_with("dob")), parse_date)
  30. #> 2. ├─dplyr::mutate_at(., vars(starts_with("dob")), parse_date)
  31. #> 3. │ ├─dplyr::mutate(.tbl, !!!funs)
  32. #> 4. │ └─dplyr:::mutate.data.frame(.tbl, !!!funs)
  33. #> 5. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
  34. #> 6. │ ├─base::withCallingHandlers(...)
  35. #> 7. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
  36. #> 8. │ └─mask$eval_all_mutate(quo)
  37. #> 9. │ └─dplyr (local) eval()
  38. #> 10. ├─readr (local) `<fn>`(dob_child1)
  39. #> 11. │ └─readr::parse_vector(...)
  40. #> 12. │ └─base::stopifnot(is.character(x))
  41. #> 13. │ └─base::stop(simpleError(msg, call = if (p <- sys.parent(1L)) sys.call(p)))
  42. #> 14. └─dplyr (local) `<fn>`(`<smplErrr>`)
  43. #> 15. └─rlang::abort(message, class = error_class, parent = parent, call = error_call)
  44. ## 从头开始 ##
  45. family <- tribble(
  46. ~family, ~dob_child1, ~dob_child2, ~gender_child1, ~gender_child2,
  47. 1L, "1998-11-26", "2000-01-29", 1L, 2L,
  48. 2L, "1996-06-22", NA, 2L, NA,
  49. 3L, "2002-07-11", "2004-04-05", 2L, 2L,
  50. 4L, "2004-10-10", "2009-08-27", 1L, 1L,
  51. 5L, "2000-12-05", "2005-02-28", 2L, 1L,
  52. )
  53. output1 <- family %>% mutate_at(vars(starts_with("dob")), parse_date)
  54. output1
  55. #> # A tibble: 5 × 5
  56. #> family dob_child1 dob_child2 gender_child1 gender_child2
  57. #> <int> <date> <date> <int> <int>
  58. #> 1 1 1998-11-26 2000-01-29 1 2
  59. #> 2 2 1996-06-22 NA 2 NA
  60. #> 3 3 2002-07-11 2004-04-05 2 2
  61. #> 4 4 2004-10-10 2009-08-27 1 1
  62. #> 5 5 2000-12-05 2005-02-28 2 1
  63. # 'across' 语法
  64. output2 <- family %>% mutate(across(starts_with("dob"), parse_date))
  65. output2
  66. #> # A tibble: 5 × 5
  67. #> family dob_child1 dob_child2 gender_child1 gender_child2
  68. #> <int> <date> <date> <int> <int>
  69. #> 1 1 1998-11-26 2000-01-29 1 2
  70. #> 2 2 1996-06-22 NA 2 NA
  71. #> 3 3 2002-07-11 2004-04-05 2 2
  72. #> 4 4 2004-10-10 2009-08-27 1 1
  73. #> 5 5 2000-12-05 2005-02-28 2 1
  74. all.equal(output1, output
  75. <details>
  76. <summary>英文:</summary>
  77. 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.
  78. ``` r
  79. library(tidyverse)
  80. family &lt;- tribble(
  81. ~family, ~dob_child1, ~dob_child2, ~gender_child1, ~gender_child2,
  82. 1L, &quot;1998-11-26&quot;, &quot;2000-01-29&quot;, 1L, 2L,
  83. 2L, &quot;1996-06-22&quot;, NA, 2L, NA,
  84. 3L, &quot;2002-07-11&quot;, &quot;2004-04-05&quot;, 2L, 2L,
  85. 4L, &quot;2004-10-10&quot;, &quot;2009-08-27&quot;, 1L, 1L,
  86. 5L, &quot;2000-12-05&quot;, &quot;2005-02-28&quot;, 2L, 1L,
  87. )
  88. family &lt;- family %&gt;% mutate_at(vars(starts_with(&quot;dob&quot;)), parse_date)
  89. family
  90. #&gt; # A tibble: 5 &#215; 5
  91. #&gt; family dob_child1 dob_child2 gender_child1 gender_child2
  92. #&gt; &lt;int&gt; &lt;date&gt; &lt;date&gt; &lt;int&gt; &lt;int&gt;
  93. #&gt; 1 1 1998-11-26 2000-01-29 1 2
  94. #&gt; 2 2 1996-06-22 NA 2 NA
  95. #&gt; 3 3 2002-07-11 2004-04-05 2 2
  96. #&gt; 4 4 2004-10-10 2009-08-27 1 1
  97. #&gt; 5 5 2000-12-05 2005-02-28 2 1
  98. # Run it again
  99. family &lt;- family %&gt;% mutate_at(vars(starts_with(&quot;dob&quot;)), parse_date)
  100. #&gt; Error in `mutate()`:
  101. #&gt; ℹ In argument: `dob_child1 = (function (x, format = &quot;&quot;, na = c(&quot;&quot;,
  102. #&gt; &quot;NA&quot;), locale = default_locale(), ...`.
  103. #&gt; Caused by error in `parse_vector()`:
  104. #&gt; ! is.character(x) is not TRUE
  105. #&gt; Backtrace:
  106. #&gt; ▆
  107. #&gt; 1. ├─family %&gt;% mutate_at(vars(starts_with(&quot;dob&quot;)), parse_date)
  108. #&gt; 2. ├─dplyr::mutate_at(., vars(starts_with(&quot;dob&quot;)), parse_date)
  109. #&gt; 3. │ ├─dplyr::mutate(.tbl, !!!funs)
  110. #&gt; 4. │ └─dplyr:::mutate.data.frame(.tbl, !!!funs)
  111. #&gt; 5. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
  112. #&gt; 6. │ ├─base::withCallingHandlers(...)
  113. #&gt; 7. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
  114. #&gt; 8. │ └─mask$eval_all_mutate(quo)
  115. #&gt; 9. │ └─dplyr (local) eval()
  116. #&gt; 10. ├─readr (local) `&lt;fn&gt;`(dob_child1)
  117. #&gt; 11. │ └─readr::parse_vector(...)
  118. #&gt; 12. │ └─base::stopifnot(is.character(x))
  119. #&gt; 13. │ └─base::stop(simpleError(msg, call = if (p &lt;- sys.parent(1L)) sys.call(p)))
  120. #&gt; 14. └─dplyr (local) `&lt;fn&gt;`(`&lt;smplErrr&gt;`)
  121. #&gt; 15. └─rlang::abort(message, class = error_class, parent = parent, call = error_call)
  122. ## start from scratch ##
  123. family &lt;- tribble(
  124. ~family, ~dob_child1, ~dob_child2, ~gender_child1, ~gender_child2,
  125. 1L, &quot;1998-11-26&quot;, &quot;2000-01-29&quot;, 1L, 2L,
  126. 2L, &quot;1996-06-22&quot;, NA, 2L, NA,
  127. 3L, &quot;2002-07-11&quot;, &quot;2004-04-05&quot;, 2L, 2L,
  128. 4L, &quot;2004-10-10&quot;, &quot;2009-08-27&quot;, 1L, 1L,
  129. 5L, &quot;2000-12-05&quot;, &quot;2005-02-28&quot;, 2L, 1L,
  130. )
  131. output1 &lt;- family %&gt;% mutate_at(vars(starts_with(&quot;dob&quot;)), parse_date)
  132. output1
  133. #&gt; # A tibble: 5 &#215; 5
  134. #&gt; family dob_child1 dob_child2 gender_child1 gender_child2
  135. #&gt; &lt;int&gt; &lt;date&gt; &lt;date&gt; &lt;int&gt; &lt;int&gt;
  136. #&gt; 1 1 1998-11-26 2000-01-29 1 2
  137. #&gt; 2 2 1996-06-22 NA 2 NA
  138. #&gt; 3 3 2002-07-11 2004-04-05 2 2
  139. #&gt; 4 4 2004-10-10 2009-08-27 1 1
  140. #&gt; 5 5 2000-12-05 2005-02-28 2 1
  141. # &#39;across&#39; syntax
  142. output2 &lt;- family %&gt;% mutate(across(starts_with(&quot;dob&quot;), parse_date))
  143. output2
  144. #&gt; # A tibble: 5 &#215; 5
  145. #&gt; family dob_child1 dob_child2 gender_child1 gender_child2
  146. #&gt; &lt;int&gt; &lt;date&gt; &lt;date&gt; &lt;int&gt; &lt;int&gt;
  147. #&gt; 1 1 1998-11-26 2000-01-29 1 2
  148. #&gt; 2 2 1996-06-22 NA 2 NA
  149. #&gt; 3 3 2002-07-11 2004-04-05 2 2
  150. #&gt; 4 4 2004-10-10 2009-08-27 1 1
  151. #&gt; 5 5 2000-12-05 2005-02-28 2 1
  152. all.equal(output1, output2)
  153. #&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:

确定