Performance (我无法击败所有现有的答案;)因为ddply只适用于数据帧,所以这个示例会产生最差的性能。我希望在将来的版本中为这种类型的常见操作提供更好的接口。仅供参考:您不能在CRAN包中使用。内部调用,请参阅。@JoshuaUlrich当答案在近2年前编写时,您可以
Performance (我无法击败所有现有的答案;)因为ddply只适用于数据帧,所以这个示例会产生最差的性能。我希望在将来的版本中为这种类型的常见操作提供更好的接口。仅供参考:您不能在CRAN包中使用。内部调用,请参阅。@JoshuaUlrich当答案在近2年前编写时,您可以,performance,r,join,merge,data.table,Performance,R,Join,Merge,Data.table,(我无法击败所有现有的答案;)因为ddply只适用于数据帧,所以这个示例会产生最差的性能。我希望在将来的版本中为这种类型的常见操作提供更好的接口。仅供参考:您不能在CRAN包中使用。内部调用,请参阅。@JoshuaUlrich当答案在近2年前编写时,您可以使用iirc。我将把这个答案更新为数据。现在表自动优化平均值(不在内部调用.Internal。@MatthewDowle:是的,我不确定它何时/是否会改变。我只知道现在是这样。你的回答很好,只是不能打包使用。@AleksandrBlekh谢谢。
(我无法击败所有现有的答案;)因为ddply只适用于数据帧,所以这个示例会产生最差的性能。我希望在将来的版本中为这种类型的常见操作提供更好的接口。仅供参考:您不能在CRAN包中使用
。内部调用,请参阅。@JoshuaUlrich当答案在近2年前编写时,您可以使用iirc。我将把这个答案更新为数据。现在表
自动优化平均值
(不在内部调用.Internal
。@MatthewDowle:是的,我不确定它何时/是否会改变。我只知道现在是这样。你的回答很好,只是不能打包使用。@AleksandrBlekh谢谢。我已将您的评论链接到现有功能请求。让我们搬到那里去。您的示例代码很好地显示了for
循环,这很好。你能为这个问题添加更多关于“SEM分析”的信息吗?例如,我猜SEM=扫描电子显微镜?了解更多关于应用程序的信息会让我们更感兴趣,并帮助我们确定优先级。更新得不错。谢谢与此数据集相比,我认为您的机器是一头野兽。。二级缓存的大小是多少(如果存在,还有三级缓存)?i7二级缓存是2x256 KB 8路,三级缓存是4 MB 16路。128 GB SSD,Dell Inspiron上的Win 7可让您重新格式化示例。我有点困惑。data.table(在本例中)比dplyr好吗?如果是,在什么情况下。问题是性能。您只提供了连接的语法。虽然有帮助,但它不能回答问题。这个答案缺乏基准数据,使用OP的例子来显示它的性能更好,或者至少具有很强的竞争力。
N <- 1e6
d1 <- data.frame(x=sample(N,N), y1=rnorm(N))
d2 <- data.frame(x=sample(N,N), y2=rnorm(N))
d <- merge(d1,d2)
# 7.6 sec
library(plyr)
d <- join(d1,d2)
# 2.9 sec
library(data.table)
dt1 <- data.table(d1, key="x")
dt2 <- data.table(d2, key="x")
d <- data.frame( dt1[dt2,list(x,y1,y2=dt2$y2)] )
# 4.9 sec
library(sqldf)
sqldf()
sqldf("create index ix1 on d1(x)")
sqldf("create index ix2 on d2(x)")
d <- sqldf("select * from d1 inner join d2 on d1.x=d2.x")
sqldf()
# 17.4 sec
system.time({
d <- d1
d$y2 <- d2$y2[match(d1$x,d2$x)]
})
DF1 = data.frame(a = c(1, 1, 2, 2), b = 1:4)
DF2 = data.frame(b = c(1, 2, 3, 3, 4), c = letters[1:5])
merge(DF1, DF2)
b a c
1 1 1 a
2 2 1 b
3 3 2 c
4 3 2 d
5 4 2 e
DF1$c = DF2$c[match(DF1$b, DF2$b)]
DF1$c
[1] a b c e
Levels: a b c d e
> DF1
a b c
1 1 1 a
2 1 2 b
3 2 3 c
4 2 4 e
library(plyr)
library(data.table)
library(sqldf)
set.seed(123)
N <- 1e5
d1 <- data.frame(x=sample(N,N), y1=rnorm(N))
d2 <- data.frame(x=sample(N,N), y2=rnorm(N))
g1 <- sample(1:1000, N, replace = TRUE)
g2<- sample(1:1000, N, replace = TRUE)
d <- data.frame(d1, g1, g2)
library(rbenchmark)
benchmark(replications = 1, order = "elapsed",
merge = merge(d1, d2),
plyr = join(d1, d2),
data.table = {
dt1 <- data.table(d1, key = "x")
dt2 <- data.table(d2, key = "x")
data.frame( dt1[dt2,list(x,y1,y2=dt2$y2)] )
},
sqldf = sqldf(c("create index ix1 on d1(x)",
"select * from main.d1 join d2 using(x)"))
)
set.seed(123)
N <- 1e5
g1 <- sample(1:1000, N, replace = TRUE)
g2<- sample(1:1000, N, replace = TRUE)
d <- data.frame(x=sample(N,N), y=rnorm(N), g1, g2)
benchmark(replications = 1, order = "elapsed",
aggregate = aggregate(d[c("x", "y")], d[c("g1", "g2")], mean),
data.table = {
dt <- data.table(d, key = "g1,g2")
dt[, colMeans(cbind(x, y)), by = "g1,g2"]
},
plyr = ddply(d, .(g1, g2), summarise, avx = mean(x), avy=mean(y)),
sqldf = sqldf(c("create index ix on d(g1, g2)",
"select g1, g2, avg(x), avg(y) from main.d group by g1, g2"))
)
Joining by: x
test replications elapsed relative user.self sys.self user.child sys.child
3 data.table 1 0.34 1.000000 0.31 0.01 NA NA
2 plyr 1 0.44 1.294118 0.39 0.02 NA NA
1 merge 1 1.17 3.441176 1.10 0.04 NA NA
4 sqldf 1 3.34 9.823529 3.24 0.04 NA NA
test replications elapsed relative user.self sys.self user.child sys.child
4 sqldf 1 2.81 1.000000 2.73 0.02 NA NA
1 aggregate 1 14.89 5.298932 14.89 0.00 NA NA
2 data.table 1 132.46 47.138790 131.70 0.08 NA NA
3 plyr 1 212.69 75.690391 211.57 0.56 NA NA
benchmark(replications = 1, order = "elapsed",
aggregate = aggregate(d[c("x", "y")], d[c("g1", "g2")], mean),
data.tableBad = {
dt <- data.table(d, key = "g1,g2")
dt[, colMeans(cbind(x, y)), by = "g1,g2"]
},
data.tableGood = {
dt <- data.table(d, key = "g1,g2")
dt[, list(mean(x),mean(y)), by = "g1,g2"]
},
plyr = ddply(d, .(g1, g2), summarise, avx = mean(x), avy=mean(y)),
sqldf = sqldf(c("create index ix on d(g1, g2)",
"select g1, g2, avg(x), avg(y) from main.d group by g1, g2"))
)
test replications elapsed relative user.self sys.self
3 data.tableGood 1 0.15 1.000 0.16 0.00
5 sqldf 1 1.01 6.733 1.01 0.00
2 data.tableBad 1 1.63 10.867 1.61 0.01
1 aggregate 1 6.40 42.667 6.38 0.00
4 plyr 1 317.97 2119.800 265.12 51.05
packageVersion("data.table")
# [1] ‘1.8.2’
packageVersion("plyr")
# [1] ‘1.7.1’
packageVersion("sqldf")
# [1] ‘0.4.6.4’
R.version.string
# R version 2.15.1 (2012-06-22)
test replications elapsed relative user.self sys.self
4 data.tableBest 1 0.532 1.000000 0.488 0.020
7 sqldf 1 2.059 3.870301 2.041 0.008
3 data.tableBetter 1 9.580 18.007519 9.213 0.220
1 aggregate 1 14.864 27.939850 13.937 0.316
2 data.tableWorst 1 152.046 285.800752 150.173 0.556
6 plyrwithInternal 1 198.283 372.712406 189.391 7.665
5 plyr 1 225.726 424.296992 208.013 8.004
test replications elapsed relative user.self sys.self
5 dplyr 1 0.25 1.00 0.25 0.00
3 data.tableGood 1 0.28 1.12 0.27 0.00
6 sqldf 1 0.58 2.32 0.57 0.00
2 data.tableBad 1 1.10 4.40 1.09 0.01
1 aggregate 1 4.79 19.16 4.73 0.02
4 plyr 1 186.70 746.80 152.11 30.27
packageVersion("data.table")
[1] ‘1.8.10’
packageVersion("plyr")
[1] ‘1.8’
packageVersion("sqldf")
[1] ‘0.4.7’
packageVersion("dplyr")
[1] ‘0.1.2’
R.version.string
[1] "R version 3.0.2 (2013-09-25)"
dplyr = summarise(dt_dt, avx = mean(x), avy = mean(y))
dt <- tbl_dt(d)
dt_dt <- group_by(dt, g1, g2)
test replications elapsed relative user.self sys.self
2 data.tableGood 1 0.02 1.0 0.02 0.00
3 dplyr 1 0.04 2.0 0.04 0.00
4 sqldf 1 0.46 23.0 0.46 0.00
1 aggregate 1 6.11 305.5 6.10 0.02
test replications elapsed relative user.self sys.self
2 data.tableGood 1 0.02 1 0.02 0.00
3 dplyr 1 0.02 1 0.02 0.00
4 sqldf 1 0.44 22 0.43 0.02
1 aggregate 1 6.14 307 6.10 0.01
packageVersion("data.table")
[1] '1.9.0'
packageVersion("dplyr")
[1] '0.1.2'
test replications elapsed relative user.self sys.self
5 dplyr 1 0.01 1 0.02 0.00
3 data.tableGood 1 0.02 2 0.01 0.00
6 sqldf 1 0.47 47 0.46 0.00
1 aggregate 1 6.16 616 6.16 0.00
2 data.tableBad 1 15.45 1545 15.38 0.01
4 plyr 1 110.23 11023 90.46 19.52
N <- 1e8
g1 <- sample(1:50000, N, replace = TRUE)
g2<- sample(1:50000, N, replace = TRUE)
d <- data.frame(x=sample(N,N), y=rnorm(N), g1, g2)
test replications elapsed relative user.self sys.self
1 dplyr 1 14.88 1 6.24 7.52
2 data.tableGood 1 28.41 1 18.55 9.4
require(data.table)
require(dplyr)
require(rbenchmark)
N <- 1e8
g1 <- sample(1:50000, N, replace = TRUE)
g2 <- sample(1:50000, N, replace = TRUE)
d <- data.frame(x=sample(N,N), y=rnorm(N), g1, g2)
benchmark(replications = 5, order = "elapsed",
data.table = {
dt <- as.data.table(d)
dt[, lapply(.SD, mean), by = "g1,g2"]
},
dplyr_DF = d %.% group_by(g1, g2) %.% summarise(avx = mean(x), avy=mean(y))
)
test replications elapsed relative user.self sys.self
1 data.table 5 15.35 1.00 13.77 1.57
2 dplyr_DF 5 137.84 8.98 136.31 1.44
Outer join: merge(x = df1, y = df2, by = "CustomerId", all = TRUE)
Left outer: merge(x = df1, y = df2, by = "CustomerId", all.x = TRUE)
Right outer: merge(x = df1, y = df2, by = "CustomerId", all.y = TRUE)
Cross join: merge(x = df1, y = df2, by = NULL)