的方式,我想知道什么是寻找在xts
对象是同一个特定行的所有行的最快的方法最快找到匹配的行
library(xts)
nRows <- 3
coreData <- data.frame(a=rnorm(nRows), b=rnorm(nRows), c=rnorm(nRows))
testXts1 <- xts(coreData, order.by=as.Date(1:nRows))
testXts2 <- xts(coreData, order.by=as.Date((nRows + 1):(2*nRows)))
testXts3 <- xts(coreData, order.by=as.Date((2*nRows + 1):(3*nRows)))
testXts <- rbind(testXts1, testXts2, testXts3)
> testXts
a b c
1970-01-02 -0.3288756 1.441799 1.321608
1970-01-03 -0.7105016 1.639239 -2.056861
1970-01-04 0.1138675 -1.782825 -1.081799
1970-01-05 -0.3288756 1.441799 1.321608
1970-01-06 -0.7105016 1.639239 -2.056861
1970-01-07 0.1138675 -1.782825 -1.081799
1970-01-08 -0.3288756 1.441799 1.321608
1970-01-09 -0.7105016 1.639239 -2.056861
1970-01-10 0.1138675 -1.782825 -1.081799
rowToSearch <- first(testXts)
> rowToSearch
a b c
1970-01-02 -0.3288756 1.441799 1.321608
indicesOfMatchingRows <- unlist(apply(testXts, 1, function(row) lapply(1:NCOL(row), function(i) row[i] == coredata(rowToSearch[, i]))))
testXts[indicesOfMatchingRows, ]
a b c
1970-01-02 -0.3288756 1.441799 1.321608
1970-01-05 -0.3288756 1.441799 1.321608
1970-01-08 -0.3288756 1.441799 1.321608
我相信这可以更优雅地完成,快速的方式。
一个更普遍的问题是,您R中是怎么说的:“我有这样的行矩阵[5]我怎么能找到矩阵是相同的矩阵[5](的指标)其他行”。
如何做到这一点的data.table
?
随着摘要,你只能做一次哈希表,所以大概如果你需要多次搜索会得到回报 – jenesaisquoi
我相信这个基准对于'roland.dt'来说太快了,因为'testDT'正在通过**被引用**到在第一次试验期间将其分类的'roland.dt',这将使后续试验更快。你可以在这里试试:'a < - data.table(x = 10:1);一个; foo < - function(x){setkey(x); X [data.table(X = 1:5)]}; B'-foo(a)的一个''或阅读[这里](https://stackoverflow.com/a/14293056/2573061) – C8H10N4O2
这并不是一个巨大的差异,但我不能比两倍的基准更好的“诚实”关闭'data.table'解决方案(下面的解决方案) – C8H10N4O2