Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify binary columns

Tags:

r

I would like to identify binary columns in a data.frame.

For example, this table

my.table <-read.table(text="a,b,c
0,2,0
0.25,1,1
1,0,0", header=TRUE, as.is=TRUE,sep = ",")

would give FALSE, FALSE, TRUE

like image 929
Pierre Lapointe Avatar asked Mar 22 '12 21:03

Pierre Lapointe


2 Answers

apply(my.table,2,function(x) { all(x %in% 0:1) })

(or

apply(my.table,2,function(x) { all(na.omit(x) %in% 0:1) })

if you want to allow for NA values)

like image 152
Ben Bolker Avatar answered Oct 01 '22 04:10

Ben Bolker


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))
#    a     b     c 
#FALSE  TRUE  TRUE 

...or if you only accept 0,1,NA:

is.binary <- function(v) {
  x <- unique(v)
  length(x) - sum(is.na(x)) == 2L && all(x[1:2] == 0:1)
}
like image 39
Tommy Avatar answered Oct 01 '22 03:10

Tommy