How does unique() work with regard to levels() ?


My code gives me this:

[1] mean_PD     mean_PD_bas mean_diff  
Levels: mean_diff mean_PD mean_PD_bas

but this is confusing as levels() gives me this:

[1] "mean_diff"   "mean_PD"     "mean_PD_bas"

The same here:

x <- factor(c("B", "A", "C", "A", "B"))

# Using unique() on the factor

unique_levels <- unique(x)

levels_in_order <- levels(x)

So what is going on with order in unique() ?

kind regards,

Hi @Andrzej,

unique doesn't change the order of the vector.

In your example

x <- factor(c("B", "A", "C", "A", "B"))

is a vector c("B", "A", "C", "A", "B") for which (by default) the levels are set to A, B, C. The reason for this is explained in the help of the function for factor ( Parameter:levels an optional vector of the unique values (as character strings) that x might have taken. The default is the unique set of values taken by as.character(x), sorted into increasing order of x. Note that this set can be specified as smaller than sort(unique(x)).)

So when you say unique(x) it will just sort the values of the factor ignoring the levels itself.
The levels function shows the order since thats the way it is stored in you x variable.

If you would do:

temp <- c("B", "A", "C", "A", "B")
# change the ordering of the levels
x <- factor(temp, levels =  sort(x = unique(temp), decreasing = TRUE))

# same as in your example

#will use the order i specified in levels

unique will stay the same as in your example and the levels function will give the values in reverse order since i defined them that way.

So if you want always to get the levels in increasing order use:

#[1] "A" "B" "C"

Hope it helps.

This is best one-liner, thank you for your kind explanation,

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.