gpt4 book ai didi

performance - 子集数据帧的最有效方法

转载 作者:行者123 更新时间:2023-12-01 05:19:46 24 4
gpt4 key购买 nike

任何人都可以提出更有效的方法来在不使用 SQL/indexing/data.table 的情况下对数据帧进行子集化选项?

我找了类似的问题,this one建议索引选项。

以下是对时间进行子集化的方法。

#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

编辑:
正如罗兰建议我使用 microbenchmark .好像 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)

enter image description here

最佳答案

正如 Roland 建议的那样,我使用了微基准测试。好像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)

enter image description here

关于performance - 子集数据帧的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17340541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com