R 中的 purrr 减少错误?

问题描述 投票:0回答:1

为了生成一个 Markdown 文档,我尝试过

reduce(as.list(letters[1:3]),sprintf,fmt="%s %s %s")

这不起作用,它会抛出错误

Error in fn(out, elt, ...) : too few arguments

我预料到了

[1] "a b c"

自从

reduce(as.list(letters[1:2]),sprintf,fmt="%s %s")

工作和输出

[1] "a b"
> sessionInfo()
> R version 4.3.2 (2023-10-31 ucrt)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
\[1\] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
\[4\] LC_NUMERIC=C                           LC_TIME=English_United States.utf8

time zone: Europe/Paris
tzcode source: internal

attached base packages:
\[1\] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
\[1\] purrr_1.0.2

loaded via a namespace (and not attached):
\[1\] compiler_4.3.2    magrittr_2.0.3    cli_3.6.2         tools_4.3.2       rstudioapi_0.15.0 vctrs_0.6.5       lifecycle_1.0.4   rlang_1.1.3

> 
r purrr
1个回答
0
投票

如果你输入 ??reduce 你会看到它会发出咕噜声:

"reduce() is an operation that combines the elements of a vector into a single value. The combination is driven by .f, a binary function that takes two values and returns a single value:"

因此,reduce 无法工作,因为它需要 2 个值,而您使用的是 3 个值。您可以尝试另一种方法:

paste(letters[1:3], collapse = " ")
© www.soinside.com 2019 - 2024. All rights reserved.