SmallChess
SmallChess

Reputation: 8106

How to check if a factor variable has only 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

Upvotes: 0

Views: 1938

Answers (2)

Sandipan Dey
Sandipan Dey

Reputation: 23101

Yet another way to do the same:

check.factor.levels <- function(expected, actual) {
  length(setdiff(actual, expected)) == 0  
}

expected <- c('FP', 'TP')
check.factor.levels(expected, levels(a))
#[1] TRUE
check.factor.levels(expected, levels(b))
#[1] TRUE
check.factor.levels(expected, levels(c))
#[1] FALSE

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388807

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

Upvotes: 5

Related Questions