从xts列表中检索非零值的列索引和行索引

时间:2017-03-10 19:18:19

标签: r xts

我的数据看起来像:

 [[1]]
 KRW.LOW.1
2014-08-08        NA
2014-08-11        -1
2014-08-12        -1
2014-08-13        -1
2014-08-14        -1

[[2]]
MYR.LOW.1
2014-08-08        NA
2014-08-11        -1
2014-08-12        -1
2014-08-13        0
2014-08-14        0

这些只是'信号'。我正在尝试检索列表索引和行索引,其中有非零条目。例如,从上面的例子我想保存[[1]] [2],[[1]] [3] ... [[2]] [2],[[2]] [3]所以我可以使用这些索引(或信号)从我的原始数据中调用相应的值。

我不知道最好的办法是什么。我在想一个for()函数,但后来我意识到这会导致单个括号indice不匹配双括号indice。建议将不胜感激。

list(structure(c(0, 0, -1, 0, 1), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
"zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "EUR.CLOSE")), 
    structure(c(-1, 0, 1, 0, -1), .indexCLASS = c("POSIXct", 
    "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "JPY.CLOSE")), 
    structure(c(-1, 0, 0, 0, 1), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "GBP.CLOSE")), 
    structure(c(0, 0, 1, 0, -1), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "CHF.OPEN")), 
    structure(c(-1, 0, 0, 1, 0), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "AUD.CLOSE")), 
    structure(c(1, 0, 1, 0, -1), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "CAD.CLOSE")), 
    structure(c(-1, 0, -1, 0, 1), .indexCLASS = c("POSIXct", 
    "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "NZD.CLOSE")), 
    structure(c(0, 1, 1, 0, 0), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "SEK.OPEN")), 
    structure(c(0, 0, 1, 0, 0), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "NOK.CLOSE")), 
    structure(c(0, 0, 1, 0, -1), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "CZK.CLOSE")), 
    structure(c(0, 0, 1, 0, 0), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "HUF.LOW.1")), 
    structure(c(-1, 0, 0, 0, 0), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "ILS.LOW.1")), 
    structure(c(1, 0, 0, 0, 0), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "PLN.LOW.1")), 
    structure(c(0, 1, 0, -1, 1), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487224800, 1487311200, 1487570400, 
    1487656800, 1487743200), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "RUB.LOW.1")), 
    structure(c(-1, -1, 0, -1, -1), .indexCLASS = c("POSIXct", 
    "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "TRY.HIGH.1")), 
    structure(c(0, 0, 0, -1, -1), .indexCLASS = c("POSIXct", 
    "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "ZAR.LOW.1")), 
    structure(c(1, 0, 0, -1, -1), .indexCLASS = c("POSIXct", 
    "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "BRL.LOW.1")), 
    structure(c(1, 0, 0, 0, 0), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "CLP.LOW.1")), 
    structure(c(1, 0, 1, 0, -1), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "COP.CLOSE")), 
    structure(c(1, -1, -1, -1, -1), .indexCLASS = c("POSIXct", 
    "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "MXN.LOW.1")), 
    structure(c(1, -1, 0, -1, 0), .indexCLASS = c("POSIXct", 
    "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "PEN.HIGH.1")), 
    structure(c(1, 1, 1, 0, -1), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "CNY.CLOSE")), 
    structure(c(0, 1, 1, 0, 0), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "IDR.LOW.1")), 
    structure(c(1, 1, 1, 1, 1), .indexCLASS = c("POSIXct", "POSIXt"
    ), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487138400, 1487224800, 1487311200, 
    1487570400, 1487743200), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "INR.LOW.1")), 
    structure(c(-1, -1, -1, -1, -1), .indexCLASS = c("POSIXct", 
    "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "CST6CDT", class = c("xts", 
    "zoo"), index = structure(c(1487311200, 1487570400, 1487656800, 
    1487743200, 1487829600), tzone = "CST6CDT", tclass = c("POSIXct", 
    "POSIXt")), .Dim = c(5L, 1L), .Dimnames = list(NULL, "KRW.LOW.1")))

1 个答案:

答案 0 :(得分:2)

L成为xts对象的输入列表。假设已经调用了library(xts)。第三种替代方案是最短的,并且不需要除了xts以外的任何软件包,无论如何都已经加载了。

1)融合/ lapply 这给出了一个两列数据框,其第一列是xts对象中的位置,第二列是列表组件的索引:

library(reshape2)

melt(lapply(L, function(x) which(coredata(x) != 0)))

2)as.data.frame.table / merge 这是另一种可能性,它提供了一个2列数据框,其第一列引用xts对象中的位置为A,B,C,等,其第二列是xts对象的列标题:

s <- subset(as.data.frame.table(coredata(do.call(merge, L) != 0)), Freq)[1:2]

列是两个因素,可以选择转换为这样的索引:

replace(s, TRUE, lapply(s, as.numeric))

3)merge / which w arr.ind 另一种可能性是:

which(do.call(merge, L) != 0, arr.ind = TRUE)