Alex Rivera | Logout

Identify binary columns

Asked 2012-03-22T21:39:50.467
12

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

r
Edit
Report

1 Answer

18
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)

answered 2012-03-22T21:42:47.697

Your Answer