Sam*_*amo 10 r xts data.table
我想知道找到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
Run Code Online (Sandbox Code Playgroud)
我相信这可以更优雅,更快捷的方式完成.
更一般的问题是你如何在R中说"我有这个行矩阵[5,]我怎样才能找到矩阵中与矩阵[5,]相同的其他行的索引".
怎么做data.table?
既然你说速度是你的主要考虑因素,那么即使使用Rcpp的data.table解决方案,你也可以获得加速:
library(Rcpp)
cppFunction(
"LogicalVector compareToRow(NumericMatrix x, NumericVector y) {
const int nr = x.nrow();
const int nc = x.ncol();
LogicalVector ret(nr, true);
for (int j=0; j < nr; ++j) {
for (int k=0; k < nc; ++k) {
if (x(j, k) != y[k]) {
ret[j] = false;
break;
}
}
}
return ret;
}")
testXts[compareToRow(testXts, rowToSearch),]
# a b c
# 1970-01-02 1.324457 0.8485654 -1.464764
# 1970-01-05 1.324457 0.8485654 -1.464764
# 1970-01-08 1.324457 0.8485654 -1.464764
Run Code Online (Sandbox Code Playgroud)
这是一个相当大的实例(有100万行)的比较:
set.seed(144)
bigXts <- testXts[sample(nrow(testXts), 1000000, replace=TRUE),]
testDT <- as.data.frame(bigXts)
josilber <- function(x, y) x[compareToRow(x, y),]
roland.base <- function(x, y) x[colSums(t(x) != as.vector(y)) == 0L,]
library(data.table)
roland.dt <- function(testDT, y) {
setDT(testDT, keep.rownames=TRUE)
setkey(testDT, a, b, c)
testDT[setDT(as.data.frame(y))]
}
library(microbenchmark)
microbenchmark(josilber(bigXts, rowToSearch), roland.base(bigXts, rowToSearch), roland.dt(testDT, rowToSearch), times=10)
# Unit: milliseconds
# expr min lq mean median uq max
# josilber(bigXts, rowToSearch) 7.830986 10.24748 45.64805 14.41775 17.37049 258.4404
# roland.base(bigXts, rowToSearch) 3530.042324 3964.72314 4288.05758 4179.64233 4534.21407 5400.5619
# roland.dt(testDT, rowToSearch) 32.826285 34.95014 102.52362 57.30213 130.51053 267.2249
Run Code Online (Sandbox Code Playgroud)
此基准测试假定在调用之前对象已被转换为数据帧(约4秒开销),roland.dt并且在调用之前compareToRows已经编译(开销约3秒)josilber.Rcpp解决方案比基本R解决方案快约300倍,比中值运行时的data.table解决方案快约4倍.基于这种方法digest没有竞争力,每次执行时间超过60秒.
这是一个更快的基础R解决方案:
ind <- colSums(t(testXts) != as.vector(rowToSearch)) == 0L
testXts[ind,]
Run Code Online (Sandbox Code Playgroud)
以下是使用data.table连接的解决方案:
library(data.table)
testDT <- as.data.frame(testXts)
setDT(testDT, keep.rownames=TRUE)
setkey(testDT, a, b, c)
testDT[setDT(as.data.frame(rowToSearch))]
Run Code Online (Sandbox Code Playgroud)
但是,在比较浮点数时我会很谨慎.