R-从XT列表中检索非零值的列和行索引

  • 本文关键字:索引 非零值 检索 XT 列表 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] [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")))

L为XTS对象的输入列表。假设library(xts)已被调用。第三个选择是最短的,并且不需要任何已加载的XT以外的任何包装。

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

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

2)as.data.frame.table/merge 另一种可能性是,它给出了一个2列数据框架,其第一列是指XTS对象中的位置为a,b,b,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,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)合并/W arr.ind 又有另一个可能性是:

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

最新更新