Skip to content

Instantly share code, notes, and snippets.

@1beb
Created March 27, 2020 00:12
Show Gist options
  • Save 1beb/843d07bae42f9d42212906369512482a to your computer and use it in GitHub Desktop.
Save 1beb/843d07bae42f9d42212906369512482a to your computer and use it in GitHub Desktop.
library(dplyr)
library(microbenchmark)
x = sample(c("less often", "1", "2", "3", "4", "4+"), 1e6, r = TRUE)
fun1 = function(x) {
case_when(
x == "less often" ~ 1L,
x %in% c("1", "2") ~ 2L,
x %in% c("3", "4") ~ 3L,
x == "4+" ~ 4L
)
}
fun2 = function(x) {
y = rep(NA_integer_, length(x)) # preallocate
y[x == "less often"] = 1L
y[x %in% c("1", "2")] = 2L
y[x %in% c("3", "4")] = 3L
y[x == "4+"] = 4L
y
}
fun3 = function(x) {
x = factor(x, levels = c("less often", "1", "2", "3", "4", "4+"))
levels(x) = c(1L, 2L, 2L, 3L, 3L, 4L)
as.integer(x)
}
fun4 = function(x) {
c(2,2,3,3,4,1)[as.numeric(as.factor(x))]
}
microbenchmark(
fun1(x),
fun2(x),
fun3(x),
fun4(x)
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# fun1(x) 125.72752 131.64690 153.78324 136.260120 185.57678 218.02885 100
# fun2(x) 53.44385 57.32536 62.91769 58.625194 61.54146 125.60717 100
# fun3(x) 7.57748 7.86945 11.33043 8.566502 10.97013 72.49452 100
# fun4(x) 21.72993 24.10765 28.01275 25.517953 27.13678 88.40717 100
all.equal(fun1(x), fun2(x))
all.equal(fun2(x), fun3(x))
all.equal(fun3(x), fun4(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment