What is the difference between the functions unique()
and fct_unique()
? There does not seem to be any difference other than fct_unique()
only works with factors, whereas unique()
works with all variables, categorical and numeric.
Apart from the difference already mention in the question that fct_unique
only works with factors
, other difference is the way they return the output.
f <- factor(c('a', 'x', 'b', 'b', 'y', 'r'))
f
#[1] a x b b y r
#Levels: a b r x y
forcats::fct_unique(f)
#[1] a b r x y
#Levels: a b r x y
unique(f)
#[1] a x b y r
#Levels: a b r x y
fct_unique
returns output in order of the factor levels whereas unique
returns in the order of their occurrence in the vector.