If you want to accept binary columns with NA in them, the following should do the trick:
is.binary <- function(v) { x <- unique(v) length(x) - sum(is.na(x)) == 2L } my.table <- data.frame(a=11:15, b=c(T,F,T,NA,T), c=c('foo',NA,'bar','bar','foo')) vapply(my.table, is.binary, logical(1))
... or if you accept only 0.1, NA:
is.binary <- function(v) { x <- unique(v) length(x) - sum(is.na(x)) == 2L && all(x[1:2] == 0:1) }
Tommy
source share