-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathAggregateFunctions.Rmd
More file actions
175 lines (135 loc) · 3.36 KB
/
AggregateFunctions.Rmd
File metadata and controls
175 lines (135 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
---
title: "AggregateFunctions"
output: github_document
---
```{r data}
d <- wrapr::build_frame(
"group" , "value" |
"a" , 1L |
"a" , 2L |
"b" , 3L |
"b" , 4L )
knitr::kable(d)
```
```{r rquery}
library("rquery")
mk_td("d", c("group", "value")) %.>%
project(.,
groupby = "group",
sum := sum(value)) %.>%
to_sql(., rquery_default_db_info()) %.>%
cat(.)
```
```{r dplyr}
library("dplyr")
packageVersion("dplyr")
dplyr_soln <- function(d) {
d %>%
group_by(group) %>%
summarize(sum = sum(value)) %>%
ungroup()
}
dplyr_soln(d)
```
```{r data_table}
library("data.table")
packageVersion("data.table")
datatable_soln <- function(d) {
dt <- data.table::as.data.table(d)
dt[, .(sum = sum(value)), by = "group"]
}
datatable_soln(d)
```
```{r dtplyr}
library("dtplyr")
packageVersion("dtplyr")
dtplyr_soln <- function(d) {
d %>%
data.table::as.data.table() %>%
group_by(group) %>%
mutate(sum = sum(value)) %>%
ungroup()
}
dplyr_soln(d)
```
```{r rqdatatable}
library("rqdatatable")
packageVersion("rqdatatable")
rqdatatable_soln <- function(d) {
d %.>%
project(.,
groupby = "group",
sum := sum(value))
}
rqdatatable_soln(d)
```
```{r baseR_lookup}
base_R_lookup_soln <- function(d) {
sums <- tapply(d$value, d$group, sum)
data.frame(group = names(sums),
sum = as.numeric(sums),
stringsAsFactors = FALSE)
}
base_R_lookup_soln(d)
```
```{r baseR_rowsum}
base_R_rowsum_soln <- function(d) {
res <- as.data.frame(rowsum(d$value, d$group))
colnames(res) <- "group"
res$sum = rownames(res)
rownames(res) <- NULL
res
}
base_R_rowsum_soln(d)
```
```{r timings}
library("microbenchmark")
mk_data <- function(nrow, nextracol, npossiblegroups) {
d <- data.frame(group = sample(paste0("g_", seq_len(npossiblegroups)), nrow, replace = TRUE),
value = rnorm(nrow),
stringsAsFactors = FALSE)
for(ci in paste0("c_", seq_len(nextracol))) {
d[[ci]] <- rnorm(nrow)
}
d
}
set.seed(235253)
d <- mk_data(100000, 10, 10000)
timings1 <- microbenchmark(
dplyr_soln = dplyr_soln(d),
datatable_soln = datatable_soln(d),
dtplyr_soln = dtplyr_soln(d),
rqdatatable_soln = rqdatatable_soln(d),
base_R_lookup_soln = base_R_lookup_soln(d),
base_R_rowsum_soln = base_R_rowsum_soln(d),
times = 10L)
print(timings1)
# now try bigger example with small number of irrelevant columns
d <- mk_data(1000000, 10, 100000)
timings2 <- microbenchmark(
dplyr_soln = dplyr_soln(d),
datatable_soln = datatable_soln(d),
dtplyr_soln = dtplyr_soln(d),
rqdatatable_soln = rqdatatable_soln(d),
base_R_lookup_soln = base_R_lookup_soln(d),
base_R_rowsum_soln = base_R_rowsum_soln(d),
times = 10L)
print(timings2)
# now try medium example with large number of irrelevant columns
# translators such as dtplyr and rqdatatable are likely sensitive to column counts
d <- mk_data(100000, 100, 10000)
timings3 <- microbenchmark(
dplyr_soln = dplyr_soln(d),
datatable_soln = datatable_soln(d),
dtplyr_soln = dtplyr_soln(d),
rqdatatable_soln = rqdatatable_soln(d),
base_R_lookup_soln = base_R_lookup_soln(d),
base_R_rowsum_soln = base_R_rowsum_soln(d),
times = 10L)
print(timings3)
```
Run on an idle Mac mini (Late 2014 model), macOS 10.13.6, 8 GB 1600 MHz DDR3.
```{r version}
date()
R.version
```