Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"no applicable method" error when calculating moving average with function filter()

Tags:

r

Could someone please be so kind and point out what the problem with this code is:

mav <- function(x, n = 5) { filter(x, rep(1 / n, n), sides = 1) }

Close <- c(21000, 23400, 26800, 21000, 23400, 26800)
SourceData <- data.frame(Close)
SourceData$CloseMA1 <- mav(SourceData$Close, n = 2)

I am getting:

Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"
like image 501
cs0815 Avatar asked Apr 12 '26 20:04

cs0815


1 Answers

Try stats::filter. You can also check environment(filter) for what package your filter comes from.

like image 199
Zheyuan Li Avatar answered Apr 14 '26 12:04

Zheyuan Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!