Re-think confusing Factor functions

Re-think confusing Factor functions.


I feel Factor manipulating functions in R are confusing.
It got easier , however, after I recognized that the Factor object consists of the actual data vector & the levels vector. (* I don't know the right name of the actual data vector & the levels vector. Those are how I call.)


I think the actual data vector is a kind of integer vector and its integer corresponds to the index of the levels vector(, though I don't know the true data structure) .
By this data structure, Factor object is efficient in its data size.


Factor manipulating functions usually manipulate only the levels vector.
If you think it also manipulates the actual vector, you'll get confused.


I'll introduce some functions and pitfalls I fell in.


* Factor Functions
I usually think of changing the levels vector without changing the apperance of Factor object.
I get levels vector by using "levels() function" ,so first I tried to use "levels()= function".
However it doesn't work as I thought,
because the integers of actual vector doesn't change and the levels vector only change,
the integers of the actual vector gets to indicate (point to) different value from what it used to.


levels(x)
=> returns the level vector.


levels(x) =
=> change the level vector only.
eg.)

f = as.factor( c("LOW", "2", "3", "4", "TOP"))

f  #  [1] LOW 2   3   4   TOP
   #  Levels: 2 3 4 LOW TOP

levels(f) = c("LOW","2","3","4","TOP")

f  #  [1] 4   LOW 2   3   TOP
   #  Levels: LOW 2 3 4 TOP


If you'd like to change the order of levels vector, without changing the appearance of Factor object, use "factor() function with levels option." 


eg.) factor() function

f2 <- as.factor( c("LOW", "2", "3", "4", "TOP"))
f2   # [1] LOW 2   3   4   TOP
     # Levels: 2 3 4 LOW TOP
f2 <- factor(f2, levels = c("LOW","2","3","4","TOP"))
f2   # [1] LOW 2   3   4   TOP
     # Levels: LOW 2 3 4 TOP


Moreover, "reorder() function" , "relevel() function with ref option" seem to manipulate both actual and levels vectors.