pmap_dbl 无法与 lambda 函数一起使用。

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

pmap_dbl is not working with lambda function

问题

我正在尝试使用pmap_dbl按行找到列的总和。如果我使用sum而不是\(x) sum(x),它可以正常工作。有任何想法吗?

pacman::p_load(tidyverse)
country_df <- tibble(
  population = c(328, 38, 30, 56, 1393, 126, 57),
  population2 = c(133, 12, 99, 83, 1033, 101, 33),
  population3 = c(89, 39, 33, 56, 193, 126, 58),
  pop = 45
)
# 正常工作
country_df |> mutate(sum = pmap_dbl(pick(1:3),sum))
#> # A tibble: 7 × 5
#>   population population2 population3   pop   sum
#>        <dbl>       <dbl>       <dbl> <dbl> <dbl>
#> 1        328         133          89    45   550
#> 2         38          12          39    45    89
#> 3         30          99          33    45   162
#> 4         56          83          56    45   195
#> 5       1393        1033         193    45  2619
#> 6        126         101         126    45   353
#> 7         57          33          58    45   148

# 不正常工作
country_df |> mutate(sum = pmap_dbl(pick(1:3), \(x) sum(x)))
#> Error in `mutate()`:
#> ℹ In argument: `sum = pmap_dbl(pick(1:3), function(x) sum(x))`.
#> Caused by error in `pmap_dbl()`:
#> ℹ In index: 1.
#> Caused by error in `.f()`:
#> ! unused arguments (population = .l[[1]][[i]], population2 = .l[[2]][[i]], population3 = .l[[3]][[i]])
#> Backtrace:
#>      ▆
#>   1. ├─dplyr::mutate(country_df, sum = pmap_dbl(pick(1:3), function(x) sum(x)))
#>   2. ├─dplyr:::mutate.data.frame(...)
#>   3. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#>   4. │   ├─base::withCallingHandlers(...)
#>   5. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#>   6. │     └─mask$eval_all_mutate(quo)
#>   7. │       └─dplyr (local) eval()
#>   8. ├─purrr::pmap_dbl(...)
#>   9. │ └─purrr:::pmap_("double", .l, .f, ..., .progress = .progress)
#>  10. │   ├─purrr:::with_indexed_errors(...)
#>  11. │   │ └─base::withCallingHandlers(...)
#>  12. │   └─purrr:::call_with_cleanup(...)
#>  13. └─base::.handleSimpleError(...)
#>  14.   └─purrr (local) h(simpleError(msg, call))
#>  15.     └─cli::cli_abort(...)
#>  16.       └─rlang::abort(...)

创建于2023-07-03,使用reprex v2.0.2

英文:

I am trying to find the sum of columns rowwise using pmap_dbl. If I use sum instead of \(x) sum(x) it is working. Any ideas?

pacman::p_load(tidyverse)
country_df &lt;- tibble(
  population = c(328, 38, 30, 56, 1393, 126, 57),
  population2 = c(133, 12, 99, 83, 1033, 101, 33),
  population3 = c(89, 39, 33, 56, 193, 126, 58),
  pop = 45
)
# working
country_df |&gt; mutate(sum = pmap_dbl(pick(1:3),sum))
#&gt; # A tibble: 7 &#215; 5
#&gt;   population population2 population3   pop   sum
#&gt;        &lt;dbl&gt;       &lt;dbl&gt;       &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1        328         133          89    45   550
#&gt; 2         38          12          39    45    89
#&gt; 3         30          99          33    45   162
#&gt; 4         56          83          56    45   195
#&gt; 5       1393        1033         193    45  2619
#&gt; 6        126         101         126    45   353
#&gt; 7         57          33          58    45   148

# not working
country_df |&gt; mutate(sum = pmap_dbl(pick(1:3), \(x) sum(x)))
#&gt; Error in `mutate()`:
#&gt; ℹ In argument: `sum = pmap_dbl(pick(1:3), function(x) sum(x))`.
#&gt; Caused by error in `pmap_dbl()`:
#&gt; ℹ In index: 1.
#&gt; Caused by error in `.f()`:
#&gt; ! unused arguments (population = .l[[1]][[i]], population2 = .l[[2]][[i]], population3 = .l[[3]][[i]])
#&gt; Backtrace:
#&gt;      ▆
#&gt;   1. ├─dplyr::mutate(country_df, sum = pmap_dbl(pick(1:3), function(x) sum(x)))
#&gt;   2. ├─dplyr:::mutate.data.frame(...)
#&gt;   3. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#&gt;   4. │   ├─base::withCallingHandlers(...)
#&gt;   5. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#&gt;   6. │     └─mask$eval_all_mutate(quo)
#&gt;   7. │       └─dplyr (local) eval()
#&gt;   8. ├─purrr::pmap_dbl(...)
#&gt;   9. │ └─purrr:::pmap_(&quot;double&quot;, .l, .f, ..., .progress = .progress)
#&gt;  10. │   ├─purrr:::with_indexed_errors(...)
#&gt;  11. │   │ └─base::withCallingHandlers(...)
#&gt;  12. │   └─purrr:::call_with_cleanup(...)
#&gt;  13. └─base::.handleSimpleError(...)
#&gt;  14.   └─purrr (local) h(simpleError(msg, call))
#&gt;  15.     └─cli::cli_abort(...)
#&gt;  16.       └─rlang::abort(...)

<sup>Created on 2023-07-03 with reprex v2.0.2</sup>

答案1

得分: 3

pmapmapmap2函数有些不同,其中您可以传递的值数量是固定的。在pmap函数中,您可以传递任意数量的参数,这些参数可以使用三个点(...)来捕获。

library(dplyr)
library(purrr)

country_df |&gt; mutate(sum = pmap_dbl(pick(1:3), \(...) sum(...)))

#  population population2 population3   pop   sum
#       <dbl>       <dbl>       <dbl> <dbl> <dbl>
#1        328         133          89    45   550
#2         38          12          39    45    89
#3         30          99          33    45   162
#4         56          83          56    45   195
#5       1393        1033         193    45  2619
#6        126         101         126    45   353
#7         57          33          58    45   148
英文:

pmap is a bit different than map or map2 functions where the number of values that you can pass is fixed. In pmap functions you can pass any number of arguments which can be captured with three dots. (...).

library(dplyr)
library(purrr)

country_df |&gt; mutate(sum = pmap_dbl(pick(1:3), \(...) sum(...)))

#  population population2 population3   pop   sum
#       &lt;dbl&gt;       &lt;dbl&gt;       &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#1        328         133          89    45   550
#2         38          12          39    45    89
#3         30          99          33    45   162
#4         56          83          56    45   195
#5       1393        1033         193    45  2619
#6        126         101         126    45   353
#7         57          33          58    45   148

huangapple
  • 本文由 发表于 2023年7月3日 21:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605097.html
匿名

发表评论

匿名网友

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

确定