Student T Student T - 1 year ago 79
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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download