Convert factor into logical datatype

Viewed 14285

I have a two levels factor in my data that I want to convert to logical

a <- str(df$y)
a
Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...

I use as.logical(df$y) to convert them into logical. However, the data turn into NA

summary(a)

      Mode    NA's 
    logical  500000

At which point do I fail to convert the data?

4 Answers

This is probably a little too late to be helpful, but I ran into a similar problem and found a fix:

as.logical(as.integer(data.frame$column))

should do the trick.

You can use == to create TRUE and FALSE values:

y = factor(c(0, 1, NA))
y == "1"
# [1] FALSE  TRUE    NA
Related