How can I rename the levels of a categorical column in a DataFrame? I tried this but it throws an error:
using DataFrames
using CategoricalArrays
df = DataFrame(x=[1, 2, 3])
df.x = categorical(df.x)
levels!(df.x, ["one", "two", "three"])
ERROR: LoadError: ArgumentError: cannot remove level 1 as it is used at position 1. Change the array element type to Union{Int64, Missing} using convert if you want to transform some levels to missing values.
You can do e.g. (to show the use of DataFramesMeta.jl):
julia> using DataFramesMeta
julia> @transform(df, :x = recode(:x, 1 => "one", 2 => "two", 3 => "tree"))
3Γ1 DataFrame
Row β x
β Catβ¦
ββββββΌββββββ
1 β one
2 β two
3 β tree
the point is that you cannot replace integers with strings in place as it would change the element type of the container. You need to create a new recoded categorical vector.