To fix the levels in an R factor
:
>>> from rpy2.robjects.vectors import FactorVector, IntVector
>>> v = FactorVector((1,2,3), levels=IntVector((3,2,1)))
>>> print(v)
[1] 1 2 3
Levels: 3 2 1
Changing a column in a DataFrame can be done with:
>>> dataf[dataf.index('a')] = v
Note: In R you are happily giving numerical values (integers) while specifying levels as strings. R does let you do so silently, but be aware that the internal representation of R "factor" vector is integer, and that mix might lead to unpleasant surprises.