# How to transform many columns of TRUE/FALSE/NA to just TRUE/FALSE?

I have a dataframe that consists of a few columns of text, and then a bunch of columns that are TRUE/FALSE or NA (they were TRUE/FALSE but I left-joined them with merge and that added NAs).

Eg:

issue # | title | body  | x     | y     | z    | lbl1 | lbl2  | lbl3  | lbl4 | lbl5
1       | blah  | blah  | blah  | blah  | blah | TRUE | FALSE | FALSE | TRUE | FALSE
2       | blah  | blah  | blah  | blah  | blah | TRUE | FALSE | FALSE | TRUE | FALSE
3       | blah  | blah  | blah  | blah  | blah | NA   | NA    | NA    | NA   | NA
4       | blah  | blah  | blah  | blah  | blah | NA   | NA    | NA    | NA   | NA
5       | blah  | blah  | blah  | blah  | blah | TRUE | FALSE | FALSE | TRUE | FALSE


I know how many columns need to not converted (and also their names), though I don't know how many label columns there are (or their names - they don't share any prefix).

I tried doing:

data[,-7] <- as.logical(isTRUE(data[,-7]))


Since this seemed to work with -1 for the same elsewhere, however my first columns all ended up as TRUE/FALSE too.

I also tried:

data[8:ncol(data)] <- sapply(data[8:ncol(data)], isTRUE)


But that resulted in everything being FALSE!

I also tried:

data[data==NA] <- FALSE


But that didn't seem to do anything (still has NAs).

I'm completely new to ML and R so please bear that in mind when answering. I don't know hardly any of the functions (or even completely understand all the syntax for selecting/replacing subsets of the dataframe as I'm trying to do here!).

I found that this works (though I'm interested if there's a better way - eg. avoiding the loop, or being able to exclude the columns by name instead of being just first 8).

for (i in 8:ncol(data)){
data[,i] <- sapply(data[,i], isTRUE)
}

• Have you tried df.fillna('FALSE')? Jun 24 '19 at 0:57
• Looks like that's from a library (h2o)? It's not available on Azure where I'm running this (and although I can probably upload it, I'm trying to keep things as simple as possible for now). Jun 24 '19 at 18:46