Personal code snippets of @tmasjc

Site powered by Hugo

Image by Mads Schmidt Rasmussen from unsplash.com

Minimal Bootstrap Theme by Zachary Betz

Check Binary True / False Columns

Aug 5, 2018 #purrr

2 methods which identify columns attributes, which are the true / false columns?

library(tidyverse)

data("mtcars")

Method 1: apply

# use apply
m1 <- apply(mtcars, 2, function(x) {
    na.omit(x) %in% c(0, 1) %>% all() 
}) %>% 
    which(. == 1) %>% 
    names()
m1
## [1] "vs" "am"

Method 2: map + which

# first identify binary columns
m2 <- map(mtcars, ~ unique(.) %>% length()) %$% 
    which(. == 2, arr.ind = TRUE) %>% 
    # then identity if true / false only
    map(~ unique(mtcars[[.]]) %in% c(0, 1) %>% all()) %$%
    which(. == 1, arr.ind = FALSE) %>% 
    # return column name only
    names()
m2
## [1] "vs" "am"

Measuring performance

library(microbenchmark)

microbenchmark(
    m1, m2, times = 1e4L
)
## Unit: nanoseconds
##  expr min lq    mean median uq    max neval
##    m1  28 38 61.6239     46 52  81125 10000
##    m2  28 39 94.5954     46 52 315601 10000