37

I'm working with wavelets on a program and I'm used the package wavelets to create the DWT of a time series using the function dwt. This function returns an object of class dwt, which is a S4 object with many slots: W, V, levels, filter, and so on.

How can I access the W's as a vector?

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
zaire90
  • 713
  • 2
  • 6
  • 11
  • how can i find the names of the slots tho? – shigeta Jan 13 '14 at 15:32
  • 4
    @shigeta You can use `slotNames`. Also note that you can visit the [R Public chat room](http://chat.stackoverflow.com/rooms/25312/r-public) if you have any clarifications – Bhargav Rao Apr 20 '16 at 10:08

2 Answers2

42

@ will let you access the slots of an S4 object.

So if your object is called wave, then wave@W should get you your vector.

Note that often the best way to do this is to not access the slot directly but rather through an accessor function (e.g. coefs() rather than digging out the coefficients with $ or @). However, often such functions do not exist so you have to access the slots directly. This will mean that your code breaks if the internal implementation changes, however.

Community
  • 1
  • 1
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
2

Ari B. Friedman's answer is good.

But please keep in mind that using @ to access the slots of an S4 object may not be a good practice. See the discuss here: Is it bad practice to access S4 objects slots directly using @?

Xiaorui Zhu
  • 161
  • 7