2

Suppose I have the following multi-series zoo object:

X.Z <- structure(c(0, 0.01, 0.01, 0, 0, 0.01), .Dim = c(3L, 2L), .Dimnames = list(
NULL, c("FTSE100", "FTALLSH")), index = structure(c(5844, 
                                                    5845, 5846), class = "Date"), class = "zoo")

I want to convert X.Z into a list of zoo objects called FTSE100 and FTALLSH. I used the following:

X.Zs <- list()
for(i in 1:2){
    X.Zs[[i]] <- X.Z[,i]
}
names(X.Zs) <- colnames(X.Z)

Is there any 'more' efficient way than the above?

My question is the reverse of this question

Community
  • 1
  • 1
mallet
  • 2,454
  • 3
  • 37
  • 64

2 Answers2

2

lapply can do it very simply

X.Zs <- lapply(X.Z,"[")

Barker
  • 2,074
  • 2
  • 17
  • 31
0

You could try something like this taken from this post:

X.Zs <- lapply(seq_len(dim(X.Z)[2L]), function(i) {x <- X.Z[, i]; class(x) <- 'zoo'; x})
names(X.Zs) <- dimnames(X.Z)[[2L]]
Community
  • 1
  • 1
Vandenman
  • 3,046
  • 20
  • 33