Search code examples
rcategorical-datar-factor

How can I swap the levels?


I have the following code in R:

a <- c("No", "Yes", "No", "Yes", "Yes")
af <- as.factor(a)

Then

levels(af)

Returns

[1] "No" "Yes"

The problem is that caret::confusionMatrix considers the first factor as the positive one, if you don't pass a positive argument.

How can I swap the levels, so that the first one is "Yes"?


Solution

  • Use the factor() function:

    a <- c("No", "Yes", "No", "Yes", "Yes")
    af <- factor(a, levels = c("Yes","No"))