1

I am struggling with extracting members from lists. The code shown below produces a list of 5 members with each member having a (sub)list of two members. I would like to extract the first members of each sub-list i.e (W1 and V1). How do I do this?

library(wavelets)

library(fGarch)

x<-rnorm(32)

spec.c <- garchSpec(model = list(omega=0.99, alpha=0.005, beta=0.005))

sim <- replicate(5, dwt(cumsum(garchSim(spec.c, n = 32)), filter="haar", 
                                    n.levels=2, boundary="reflection"))

Many thanks.

John Paul
  • 12,196
  • 6
  • 55
  • 75
user2633313
  • 119
  • 8

1 Answers1

1

Try this...

W <- lapply( sim , function(x) `@`( x , W)[[1]] )
V <- lapply( sim , function(x) `@`( x , V)[[1]] )

You'll get a list of the first W of each of the 5 top-level lists and the first V of each of the 5 top-level lists.

The @ operator is used to access named slots in an S4 type object.

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • This worked nicely but what if I want to make a calculation (or say, set to zero) all the W1's without extracting them from the list? – user2633313 Aug 02 '13 at 19:07