任何人都可以建议在不使用的情况下对数据框进行子集化的更有效方法SQL/indexing/data.table
选项?
我寻找了类似的问题,并且this one https://stackoverflow.com/questions/12479238/data-frame-subset-performance建议索引选项。
以下是对时间进行子集化的方法。
#Dummy data
dat <- data.frame(x = runif(1000000, 1, 1000), y=runif(1000000, 1, 1000))
#Subset and time
system.time(x <- dat[dat$x > 500, ])
# user system elapsed
# 0.092 0.000 0.090
system.time(x <- dat[which(dat$x > 500), ])
# user system elapsed
# 0.040 0.032 0.070
system.time(x <- subset(dat, x > 500))
# user system elapsed
# 0.108 0.004 0.109
EDIT:正如罗兰建议我用的微基准测试 http://cran.r-project.org/web/packages/microbenchmark/index.html。它似乎which
表现最好。
library("ggplot2")
library("microbenchmark")
#Dummy data
dat <- data.frame(x = runif(1000000, 1, 1000), y=runif(1000000, 1, 1000))
#Benchmark
res <- microbenchmark( dat[dat$x > 500, ],
dat[which(dat$x > 500), ],
subset(dat, x > 500))
#plot
autoplot.microbenchmark(res)