目前,R 解释器将对filter 的调用调度到dplyr 环境,至少如果对象的类在可用方法中:
方法(过滤器)
[1] filter.data.frame* filter.default* filter.sf* filter.tbl_cube* filter.tbl_df* filter.tbl_lazy*
[7] 过滤器.ts*
如您所见,有一个 ts 方法,因此如果对象属于该类,解释器将改为将 x 值传递给它。但是,dplyr 的作者似乎已经阻止了该机制,而是设置了警告功能。您需要使用:
getFromNamespace('filter', 'stats')
function (x, filter, method = c("convolution", "recursive"),
sides = 2L, circular = FALSE, init = NULL)
{ <omitting rest of function body> }
# same result also obtained with:
stats::filter
R 函数包含在命名空间中,因此函数的完整名称应为:namespace_name::function_name。命名空间容器的层次结构(实际上是 R 术语中的“环境”)沿着搜索路径排列(根据加载包及其依赖项的顺序而有所不同)。 ::-infix-operator 可用于指定名称空间或包名称,该名称空间或包名称位于搜索路径的上方,而不是在调用函数的上下文中可能找到的位置。函数search 可以显示当前加载的包的名称及其关联的命名空间。见?search这是我目前的(这是一个相当臃肿的问题,因为我回答了很多问题并且通常不会从干净的系统开始:
> search()
[1] ".GlobalEnv" "package:kernlab" "package:mice" "package:plotrix"
[5] "package:survey" "package:Matrix" "package:grid" "package:DHARMa"
[9] "package:eha" "train" "package:SPARQL" "package:RCurl"
[13] "package:XML" "package:rnaturalearthdata" "package:rnaturalearth" "package:sf"
[17] "package:plotly" "package:rms" "package:SparseM" "package:Hmisc"
[21] "package:Formula" "package:survival" "package:lattice" "package:remotes"
[25] "package:forcats" "package:stringr" "package:dplyr" "package:purrr"
[29] "package:readr" "package:tidyr" "package:tibble" "package:ggplot2"
[33] "package:tidyverse" "tools:rstudio" "package:stats" "package:graphics"
[37] "package:grDevices" "package:utils" "package:datasets" "package:methods"
[41] "Autoloads"
目前我可以使用帮助系统找到 3 个版本的过滤器的实例:
?filter
# brings this up in the help panel
Help on topic 'filter' was found in the following packages:
Return rows with matching conditions
(in package dplyr in library /home/david/R/x86_64-pc-linux-gnu-library/3.5.1)
Linear Filtering on a Time Series
(in package stats in library /usr/lib/R/library)
Objects exported from other packages
(in package plotly in library /home/david/R/x86_64-pc-linux-gnu-library/3.5.1)