英文:
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 <- 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 |> 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
# not working
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(...)
<sup>Created on 2023-07-03 with reprex v2.0.2</sup>
答案1
得分: 3
pmap
与map
或map2
函数有些不同,其中您可以传递的值数量是固定的。在pmap
函数中,您可以传递任意数量的参数,这些参数可以使用三个点(...
)来捕获。
library(dplyr)
library(purrr)
country_df |> 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 |> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论