如何停止slide()函数将数值向量计算为列表? [英] How to stop slide() function from compute a numeric vector into a list?

查看:42
本文介绍了如何停止slide()函数将数值向量计算为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列的 data.frame :

Price <- c(1, 2, 5, 3, 1, 4, 7, 10, 6)
df <- data.frame(Price)

我想计算每个七个数字的最大值,结果是:

I want to calculate the maximum value of each seven numbers, resulting in:

df$MaxPrice <- c(1, 2, 5, 5, 5, 5, 7, 10, 10)

但是,当我尝试使用 mutate() slide()计算此新列时,它将在数据框内返回一个列表,而不是数值变量:

However, when I try to compute this new column with mutate() and slide(), it returns me a list inside the dataframe, instead of a numeric variable:

library(dplyr)
library(slider)

df <- df %>% 
  mutate(MaxPrice = slide(Price, max, .before = 7, .after = 0, .complete = F))

为什么会发生这种情况,以及如何使 slide()返回数字变量?

Why is this happening, and how to make slide() return a numeric variable?

推荐答案

似乎默认方法需要 list 输出.根据?slide

It appears that the default method calls for the list output. According to ?slide

vec_ptype(slide(.x))== list()

vec_ptype(slide(.x)) == list()

.ptype 的描述为

.ptype-[vector(0)/NULL]

.ptype - [vector(0) / NULL]

与输出类型相对应的原型.

A prototype corresponding to the type of the output.

如果为NULL(默认值),则通过对.f的调用结果中的通用类型进行计算来确定输出类型.

If NULL, the default, the output type is determined by computing the common type across the results of the calls to .f.

如果提供,则每次调用.f的结果都将强制转换为该类型,最终输出将具有该类型.

If supplied, the result of each call to .f will be cast to that type, and the final output will have that type.

如果getOption("vctrs.no_guessing")为TRUE,则必须提供.ptype.这是使生产代码需要固定类型的一种方法.

If getOption("vctrs.no_guessing") is TRUE, the .ptype must be supplied. This is a way to make production code demand fixed types.

本质上基于源代码(如下),默认情况下它返回一个 list ,除非我们选择所描述的特定方法(即 _vec),否则似乎没有防止它的选项. _dbl

In essence based on the source code (below), it is by default returning a list and there seems to be no option to prevent that unless we opt for specific methods described i.e. _vec or _dbl

我们可以展平

library(dplyr)
library(slider)
library(purrr)
out <- df %>% 
    mutate(MaxPrice = slide(Price, max, .before = 7, .after = 0,
       .complete = FALSE) %>% flatten_dbl) 

str(out)
#'data.frame':  9 obs. of  2 variables:
# $ Price   : num  1 2 5 3 1 4 7 10 6
# $ MaxPrice: num  1 2 5 5 5 5 7 10 10


或使用特定于类型的方法,即 slide_dbl

out <- df %>% 
    mutate(MaxPrice = slide_dbl(Price, max, .before = 7, .after = 0,
       .complete = FALSE) )

str(out)
#'data.frame':  9 obs. of  2 variables:
# $ Price   : num  1 2 5 3 1 4 7 10 6
# $ MaxPrice: num  1 2 5 5 5 5 7 10 10


如果我们检查 slide 的源代码,它将调用 slide_impl ,并且假定 .ptype list ,并且没有任何选项可以在幻灯片


If we check the source code of slide, it calls slide_impl and it assumes that .ptype as list and there is no option to pass that info in slide

slide
function (.x, .f, ..., .before = 0L, .after = 0L, .step = 1L, 
    .complete = FALSE) 
{
    slide_impl(.x, .f, ..., .before = .before, .after = .after, 
        .step = .step, .complete = .complete, .ptype = list(), 
        .constrain = FALSE, .atomic = FALSE)
}

现在,将其与 _dbl 方法

slide_dbl
function (.x, .f, ..., .before = 0L, .after = 0L, .step = 1L, 
    .complete = FALSE) 
{
    slide_vec_direct(.x, .f, ..., .before = .before, .after = .after, 
        .step = .step, .complete = .complete, .ptype = double())
}

这篇关于如何停止slide()函数将数值向量计算为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆