Student T Student T - 6 months ago 28
R Question

How to check if a factor variable has exactly the levels I want?

What's the simplest way to verify a factor variable has the levels I want to?

# I want to make sure a factor variable has 'FP', 'TP' but nothing else

a <- factor(c('TP','TP','FP')) # Return TRUE
b <- factor(c('TP')) # Return TRUE
c <- factor(c('TP', '1234')) # Return FALSE


We can use all with %in%

all(levels(a) %in% c("FP", "TP"))
#[1] TRUE
all(levels(b) %in% c("FP", "TP"))
#[1] TRUE
all(levels(c) %in% c("FP", "TP"))
#[1] FALSE

Just to avoid the repetition of code or in case if there are more levels that we need to check

checkFactor <- c("FP", "TP")
all(levels(a) %in% checkFactor)
#[1] TRUE
all(levels(b) %in% checkFactor)
#[1] TRUE
all(levels(c) %in% checkFactor)
#[1] FALSE