假设我有以下多系列zoo对象:
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")我想将X.Z转换为名为FTSE100和FTALLSH的zoo对象列表。我使用了以下代码:
X.Zs <- list()
for(i in 1:2){
X.Zs[[i]] <- X.Z[,i]
}
names(X.Zs) <- colnames(X.Z)还有比上面更“有效”的方法吗?
我的问题与this question相反
发布于 2016-08-30 11:37:19
lapply可以非常简单地做到这一点
X.Zs <- lapply(X.Z,"[")
发布于 2016-08-30 07:06:34
您可以尝试从这个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]]https://stackoverflow.com/questions/39216267
复制相似问题