0
$\begingroup$

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!).

$\endgroup$
0
$\begingroup$

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)
}
$\endgroup$
2
  • $\begingroup$ Have you tried df.fillna('FALSE')? $\endgroup$ Jun 24 '19 at 0:57
  • $\begingroup$ 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). $\endgroup$ Jun 24 '19 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.