我想对data.table个对象进行移动平均(最好是女巫自定义权重).frollmeanfrollapply(特别是)甚至TTR::EMA(不允许自定义权重,而只允许Wilder=T/F)要快得多--最后进行性能比较.我try 使用data.table:::froll(frollmean使用它,并且不会导出).它的第一个参数必须是函数的(字符)名称(对于frollmean,它是mean).

data.table:::froll('f.roll', x$c, 10)
Error in data.table:::froll("f.roll", x$c, 10) : 
  Internal error: invalid fun argument in rolling function, should have been caught before. please report to data.table issue tracker.

我try 将data.table包作为一个环境进行访问:

e.dt <- as.environment('package:data.table')
e.dt$froll <- data.table:::froll
Error in e.dt$froll <- data.table:::froll (from c.ta.R#287) : 
  cannot add bindings to a locked environment
e.dt2 <- new.env(parent=e.dt)
e.dt2$froll <- data.table:::froll
e.dt2$froll('f.roll', top.., n, na.rm=T)
Error in e.dt2$froll("f.roll", x$c, n, na.rm = T) : 
  Internal error: invalid fun argument in rolling function, should have been caught before. please report to data.table issue tracker.

我还try 将我的定制函数f.roll附加到e.dt2:

e.dt2$f.roll <- f.roll
e.dt2$froll('f.roll', top.., n, na.rm=T)
Error in e.dt2$froll("f.roll", top.., n, na.rm = T) : 
  Internal error: invalid fun argument in rolling function, should have been caught before. please report to data.table issue tracker.

froll调用C函数(CfrollfunR):

data.table:::froll
function (fun, x, n, fill = NA, algo = c("fast", "exact"), align = c("right", 
    "left", "center"), na.rm = FALSE, hasNA = NA, adaptive = FALSE) 
{
    stopifnot(!missing(fun), is.character(fun), length(fun) == 
        1L, !is.na(fun))
    algo = match.arg(algo)
    align = match.arg(align)
    ans = .Call(CfrollfunR, fun, x, n, fill, algo, align, na.rm, 
        hasNA, adaptive)
    ans
}
<bytecode: 0x0000000013738b40>
<environment: namespace:data.table>

data.table:::CfrollfunR
$name
[1] "CfrollfunR"

$address
<pointer: 0x000000001ef93e80>
attr(,"class")
[1] "RegisteredNativeSymbol"

$dll
DLL name: datatable
Filename: C:/bin/cygwin/cygwin64/home/Adi/R/win-library/4.0/data.table/libs/x64/datatable.dll
Dynamic lookup: FALSE

$numParameters
[1] -1

attr(,"class")
[1] "CallRoutine"      "NativeSymbolInfo"

然而,虽然我的定制函数f.roll不起作用,但作为第一个参数的mean起作用:

tail(e.dt2$froll('mean', x$c, 10, na.rm=T))
[1] 43.506 43.148 42.855 42.548 42.331 42.200

我判断了data.table DLLC:\bin\cygwin\cygwin64\home\Adi\R\win-library\4.0\data.table\libs\x64\datatable.dll(使用DLL Export Viewer),但它没有显示任何名为mean的函数.data.table包也没有任何mean函数(导出或未导出).

data.table:::mean
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) : 
  object 'mean' not found
data.table::mean
Error: 'mean' is not an exported object from 'namespace:data.table'

任何建议都将不胜感激.

PS以下是从microbenchmark开始的性能比较,以供参考:

## Unit: milliseconds
##        expr         min          lq        mean      median          uq         max neval
##  frollapply 131736.6469 131736.6469 131736.6469 131736.6469 131736.6469 131736.6469     1
##         EMA    262.9931    262.9931    262.9931    262.9931    262.9931    262.9931     1
##   frollmean     97.0388     97.0388     97.0388     97.0388     97.0388     97.0388     1

PS因为froll不是我的问题的解决方案(正如@Waldi指出的那样)我正在寻找替代的解决方案,但我保持现状,问了一个单独的新问题:fast way to calculate moving average/rolling function which allows custom weights

推荐答案

frollC source code,表明此内部函数仅针对meansum设计,错误与环境无关:

enum {MEAN, SUM} sfun;
  if (!strcmp(CHAR(STRING_ELT(fun, 0)), "mean")) {
    sfun = MEAN;
  } else if (!strcmp(CHAR(STRING_ELT(fun, 0)), "sum")) {
    sfun = SUM;
  } else {
    error(_("Internal error: invalid %s argument in %s function should have been caught earlier. Please report to the data.table issue tracker."), "fun", "rolling"); // # nocov
  }

R相关问答推荐

如何将具有重复名称的收件箱合并到R中的另一列中,而结果不同?

根据R中两个变量的两个条件删除带有dspirr的行

查找图下的面积

从R导出全局环境中的所有sf(numrames)对象

我不能在docker中加载sf

如何直接从Fortran到R的数组大小?

S用事件解决物质平衡问题

从一个列表的框架中移除列表包装器

Select 季度月值

如何通过匹配R中所有可能的组合来从宽到长旋转多个列?

过滤名称以特定字符串开头的文件

`夹心::vcovCL`不等于`AER::tobit`标准错误

根据纬度和距离连接两个数据集

优化从每个面的栅格中提取值

将统计检验添加到GGPUBR中的盒图,在R

构建一个6/49彩票模拟系统

使用其他DF中的文件名将列表中的每个元素保存到文件中

使用dqur在不同变量上创建具有多个条件的变量

有没有办法更改ggplot2中第二个y轴的比例限制?

基于已有ID列创建唯一ID