kaix
kaix

Reputation: 305

How to use apply to get hpfilter for multiple column?

I am trying to get the hpfilter for multiple columns of data that I have with this code, but I got an error "Error in is.ts(x) : argument "x" is missing, with no default". I would appreciate any suggestion on how to get the hpfilter for multiple column. Thank you.

 hp_filter1 <- apply(dataset[,2:ncol(dataset)],2, hpfilter(freq=1600, type="lambda"))

Upvotes: 0

Views: 218

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

You can use apply as -

hp_filter1 <- apply(dataset[, -1],2, hpfilter, freq=1600, type="lambda")

Or to correct your approach use an anonymous function which will be clearer.

hp_filter1 <- apply(dataset[,2:ncol(dataset)],2, function(x) 
                    hpfilter(x, freq=1600, type="lambda"))

Since you are applying the function to each column you can also use lapply/sapply.

Upvotes: 1

Related Questions