在单个链中获得最大日期 - n 年
In a single chain get a max date - n years
(欢迎推荐更好的标题措辞)
pacman::p_load(tidyverse, fable, fpp3, feasts)
aus_livestock$Month |> max() |> as_date()
[1] "2018-12-01"
我想在一行中得到这个日期减去 6 年。尝试过:
aus_livestock$Month |> max() |> as_date() |> -years(6)
Error: function '-' not supported in RHS call of a pipe
然后尝试花括号{}
:
aus_livestock$Month |> max() |> as_date() |> {. -years(6)}
Error: function '{' not supported in RHS call of a pipe
这确实有效:
x <- aus_livestock$Month |> max() |> as_date()
x - years(6)
[1] "2012-12-01"
但这涉及保存一个中间变量x。如果有办法,我更愿意一次性完成?
我们可以使用 lambda 函数
aus_livestock$Month |>
max() |>
as_date() |>
(\(x) x - years(6))()
[1] "2012-12-01"
(欢迎推荐更好的标题措辞)
pacman::p_load(tidyverse, fable, fpp3, feasts)
aus_livestock$Month |> max() |> as_date()
[1] "2018-12-01"
我想在一行中得到这个日期减去 6 年。尝试过:
aus_livestock$Month |> max() |> as_date() |> -years(6)
Error: function '-' not supported in RHS call of a pipe
然后尝试花括号{}
:
aus_livestock$Month |> max() |> as_date() |> {. -years(6)}
Error: function '{' not supported in RHS call of a pipe
这确实有效:
x <- aus_livestock$Month |> max() |> as_date()
x - years(6)
[1] "2012-12-01"
但这涉及保存一个中间变量x。如果有办法,我更愿意一次性完成?
我们可以使用 lambda 函数
aus_livestock$Month |>
max() |>
as_date() |>
(\(x) x - years(6))()
[1] "2012-12-01"