A random collection of R tips & tricks. Updated infrequently.

Timing and benchmarking

  • system.time() — timing blocks of code
  • Rprof() — timing execution of R functions
  • Rprofmem() — reporting memory allocation in R
  • tracemem() — detect when a copy of an R object is created

Initialising Lists/Vectors

If the size of a list/vector is known, it is usually benefitial to initialise it prior to using it rather than extending it, see below.

no_init <- function(n) {
    x <- c()
    for (i in 1:n) {
        x <- c(x, i)
    }
    x
}

with_init <- function(n) {
    x <- integer(n)
    for (i in 1:n) {
        x[i] <- i
    }
    x
}
library(rbenchmark)
n <- 1000

benchmark(no_init(n), with_init(n), 
          columns=c("test", "replications", "elapsed", "relative"))

#           test replications elapsed relative
# 1   no_init(n)          100   0.162    20.25
# 2 with_init(n)          100   0.008     1.00

Determine number of observations required to obtain target power of observation

For A/B test, determine number of observations required to make decision based on group probabilities and desired significance level & power.

power.prop.test(p1=0.33, 
                p2=0.3, 
                power=0.8, 
                alternative='two.sided', 
                sig.level=0.05)$n

Vary use of parameters depending on question to be answered (one must be NULL).