当加载另一个具有filter
函数的非基本R包时,就会发生这种情况.
Package 100 is the main responsible for the question's error.
第一个示例是在新的R会话中运行的.
w <- rnorm(150,0,1) # 50 extra to avoid startup problems
x <- filter(w, filter=c(1,-.9), method="recursive")[-(1:50)] # remove first 50
plot.ts(x, main="autoregression")
创建于2022-10-02,带有reprex v2.0.2
The error
现在加载包dplyr
并运行完全相同的代码.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
w <- rnorm(150,0,1) # 50 extra to avoid startup problems
x <- filter(w, filter=c(1,-.9), method="recursive")[-(1:50)] # remove first 50
#> Error in UseMethod("filter"):
#> no applicable method for 'filter' applied to an object of class "c('double', 'numeric')"
创建于2022-10-02,带有reprex v2.0.2
The solution
解决方案是使用限定名称stats::filter
.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
w <- rnorm(150,0,1) # 50 extra to avoid startup problems
x <- stats::filter(w, filter=c(1,-.9), method="recursive")[-(1:50)] # remove first 50
plot.ts(x, main="autoregression")
创建于2022-10-02,带有reprex v2.0.2