[英]How R differentiates between the two filter function one in dplyr package and other for linear filtering in time series?
我想根据某些条件过滤数据集。 当我查看过滤器功能的帮助时,结果是:
filter {stats} R Documentation
Linear Filtering on a Time Series
Description
Applies linear filtering to a univariate time series or to each series separately of a multivariate time series.
在网上搜索后,我找到了我需要的过滤功能,即来自 dplyr 包。 R怎么会有两个同名的函数。 我在这里缺少什么?
目前,R 解释器将调用filter
到dplyr
环境,至少如果对象的类在dplyr
方法中:
方法(过滤器)[1] filter.data.frame* filter.default* filter.sf* filter.tbl_cube* filter.tbl_df* filter.tbl_lazy*
[7] filter.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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.