Benchmark study: Modifying data within groups¶
The following makes use of the packages data.table, dplyr, memisc, rbenchmark. You may need to install them from CRAN using the code install.packages(c("data.table","dplyr","memisc","rbenchmark"))
if you want to run this on your computer. (The packages are already installed on the notebook container, however.)
library(data.table)
library(memisc)
library(dplyr)
library(rbenchmark)
load("BData.RData")
load("SData.RData")
grouped_modification_benchmark_1 < benchmark(
within =
within(BDataF,{
X1c < X1  ave(X1, a,b,FUN = mean)
X2c2 < (X2  ave(X2, a,b,FUN = mean))^2
X3ca < abs(X3  ave(X3,a,b,FUN = median))
X4cm < X4  ave(X4,a,b,FUN = max)
}),
data.table =
BDataT[,`:=`(X1c = X1  mean(X1),
X2c2 = (X2  mean(X2))^2,
X3ca = abs(X3  median(X3)),
X4cm = X4  max(X4)),
by = .(a,b)],
`group_by + mutate` =
BDTbl %>% group_by(a,b) %>%
mutate(X1c = X1  mean(X1),
X2c2 = (X2  mean(X2))^2,
X3ca = abs(X3  median(X3)),
X4cm = X4  max(X4)),
withinGroups =
withinGroups(BDataF, ~a+b, {
X1c < X1  mean(X1)
X2c2 < (X2  mean(X2))^2
X3ca < abs(X3  median(X3))
X4cm < X4  max(X4)
}),
columns = c("test","user.self","relative"),
replications = 100,
order = NULL,
relative = "user.self"
)
grouped_modification_benchmark_2 < benchmark(
within =
within(SDataF,{
X1c < X1  ave(X1, a,b,FUN = mean)
X2c2 < (X2  ave(X2, a,b,FUN = mean))^2
X3ca < abs(X3  ave(X3,a,b,FUN = median))
X4cm < X4  ave(X4,a,b,FUN = max)
}),
data.table =
SDataT[,`:=`(X1c = X1  mean(X1),
X2c2 = (X2  mean(X2))^2,
X3ca = abs(X3  median(X3)),
X4cm = X4  max(X4)),
by = .(a,b)],
`group_by + mutate` =
SDTbl %>% group_by(a,b) %>%
mutate(X1c = X1  mean(X1),
X2c2 = (X2  mean(X2))^2,
X3ca = abs(X3  median(X3)),
X4cm = X4  max(X4)),
withinGroups =
withinGroups(SDataF, ~a+b, {
X1c < X1  mean(X1)
X2c2 < (X2  mean(X2))^2
X3ca < abs(X3  median(X3))
X4cm < X4  max(X4)
}),
columns = c("test","user.self","relative"),
replications = 100,
order = NULL,
relative = "user.self"
)
save(grouped_modification_benchmark_1,
grouped_modification_benchmark_2,
file="groupedmodificationbenchmark.RData")
Downloadable R script and interactive version
 R Script: benchmarkgroupdatamodification.R
 Interactive version (shuts down after 60s):
 Interactive version (sign in required):
Explanation
The link with the “jupyterhub” icon directs you to an interactive Jupyter^{1} notebook, which runs inside a Docker container^{2}. There are two variants of the interative notebook. One shuts down after 60 seconds and does not require a sign it. The other requires sign in using your ORCID^{3} credentials, yet shuts down only after 24 hours. (There is no guarantee that such a container persists that long, it may be shut down earlier for maintenance purposes.) After shutdown all data within the container will be reset, i.e. all files created by the user will be deleted.^{4}
Above you see a rendered version of the Jupyter notebook.^{5}
 1

For more information about Jupyter see
http://jupyter.org
. The Jupyter notebooks make use of the IRKernel package.  2

For more information about Docker see
https://docs.docker.com/
. The container images were created with repo2docker, while containers are run with docker spawner.  3

ORCID is a free service for the authentication of researchers. It also allows to showcase publications and contributions to the academic community such as peer review.. See
https://info.orcid.org/whatisorcid/
for more information.  4

The Jupyter notebooks come with NO WARRANTY whatsoever. They are provided for educational and illustrative purposes only. Do not use them for production work.
 5

The notebook is rendered with the help of the nbsphinx extension.