At which point do I fail to convert the data?
I'd argue that you at no point fail to convert the data, it's the function that is a bit odd and fails to understand the nature of your data.
If you read ?as.logical
you'll see that when input is factor the levels (which are character) are used in the conversion. The only valid character strings are all variations of "true" and "false", everything else, including "0" and "1", returns NA. 0 and 1 are however interpreted as FALSE
and TRUE
, respectively, when they are given as numeric, hence all the following works:
y <- factor(c(0, 1, 1, 0))
as.logical(as.integer(levels(y)[y]))
as.logical(as.integer(y) - 1L)
as.logical(as.integer(as.character(y)))
A bit cumbersome, I know, but that's how it is.
as.logical(as.integer(levels(a))[a])
. You can read about it in the "Value" section of?as.logical
– Awlworty <- factor(0:1); str(y); as.logical(as.integer(y) - 1L)
returns[1] FALSE TRUE
– Quiltdf$y == "1"
. The result will be your logical vector. – Leer