knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
library(wr.data.table)
library(data.table)
library(dplyr)
dt <- data.table(mtcars)

DTkeeprownames <- function(dt, n) {
  for (i in 1:n) {
    dt <- rbind(dt, dt)
  }
  data.table(dt)
}

manyRows <- lapply(c(2,5,10),function(x){DTkeeprownames(dt, x)})
#str(manyRows)


dtwithmanycols <- function(dt,n ) {
  for (i in 1:n) {
    dt <- cbind(dt, dt)
  }
  data.table(dt, check.names = TRUE)
}

manyCols <- lapply(c(2,5,10), function(x){dtwithmanycols(dt, x)})
#str(manyCols)

getDT <- function(x) {
  x[1]
}
# setorder
result_setorder_rows <- purrr::map(manyRows, function(x) {
  microbenchmark::microbenchmark(
    setorder(getDT(x), am, vs),
    getDT(x) |> arrange(am, vs))
})
#result_setorder_rows
purrr::map(result_setorder_rows, boxplot)
result_setorder_cols <- purrr::map(manyCols, function(x) {
  microbenchmark::microbenchmark(
    setorder(getDT(x), am, vs),
    getDT(x) |> dplyr::arrange(am, vs))
})
#result_setorder_cols
purrr::map(result_setorder_cols, boxplot)
result_condense_rows <- purrr::map(manyRows, function(x){
  microbenchmark::microbenchmark(
    set_group(getDT(x), gear, am) |> condense(mu_mpg=mean(mpg),sd_mpg=sd(mpg)),
    getDT(x)[,.(mu_mpg=mean(mpg),sd_mpg=sd(mpg)),by=list(gear,am)],
    getDT(x) |> group_by(gear, am) |> summarise(mu_mpg=mean(mpg),sd_mpg=sd(mpg), .groups="drop")
  )
})
result_condense_rows
purrr::map(result_condense_rows, boxplot)
result_condense_cols <- purrr::map(manyCols, function(x){
  microbenchmark::microbenchmark(
    set_group(getDT(x), gear, am) |> condense(mu_mpg=mean(mpg),sd_mpg=sd(mpg)),
    getDT(x)[,.(mu_mpg=mean(mpg),sd_mpg=sd(mpg)),by=list(gear,am)],
    getDT(x) |> group_by(gear, am) |> summarise(mu_mpg=mean(mpg),sd_mpg=sd(mpg), .groups="drop")
  )
})
result_condense_cols
purrr::map(result_condense_cols, boxplot)
result_condense_cols <- purrr::map(manyCols, function(x){
  microbenchmark::microbenchmark(
    set_group(getDT(x), gear, am) |> condense(mu_mpg=mean(mpg),sd_mpg=sd(mpg)),
    getDT(x)[,.(mu_mpg=mean(mpg),sd_mpg=sd(mpg)),by=list(gear,am)],
    getDT(x) |> group_by(gear, am) |> summarise(mu_mpg=mean(mpg),sd_mpg=sd(mpg), .groups="drop")
  )
})
result_condense_cols
purrr::map(result_condense_cols, boxplot)
myvar <- "mpg"
result_def_cols <- purrr::map(manyCols, function(x){
  microbenchmark::microbenchmark(
    getDT(x) |> def_cols(mpggreaterthan19 ~ dt[[myvar]] > 19),
    getDT(x)[,.(mpggreaterthan19=mpg>19)],
    getDT(x) |> mutate(mpggreaterthan19=mpg>19)
  )
})
result_def_cols
purrr::map(result_def_cols, boxplot)


kleu046/wr.data.table documentation built on April 25, 2022, 1:41 p.m.