I have imported an excel spreadsheet to julia as a dataframe. After converting one of its columns into a categorical variable, I am trying to change the levels’ order but I can’t.
using DataFrames
using DataFramesMeta
using XLSX
using CategoricalArrays
Data= DataFrame(XLSX.readtable("feedback.xlsx", "MG"))
Once the dataframe has been created, I use DataFramesMeta to convert the column “expectations” into a categorical variable:
@transform(Data, :expectations = categorical(:expectations; ordered=true))
levels(Data.expectations)
5-element Vector{Any}:
“Excellent”
“Fair”
“Good”
“Poor”
“Very good”
I would like to reorder the levels from Excellent to Poor: Excellent, Very good, Good, Fair, Poor.
I thought the following line would work but it doesn´t:
levels!(Data.expectations, ["Excellent", "Very good", "Good", "Fair", "Poor"])
ERROR: MethodError: no method matching levels!(::Vector{Any}, ::Vector{String})
Closest candidates are:
levels!(::CategoricalPool{S, R}, ::Vector; checkunique) where {S, R} at ~/.julia/packages/CategoricalArrays/tJ8hD/src/pool.jl:250
levels!(::CategoricalArray{T, N, R}, ::AbstractVector; allowmissing, allow_missing) where {T, N, R} at ~/.julia/packages/CategoricalArrays/tJ8hD/src/array.jl:793
levels!(::SubArray{T, N, P}, ::Vector) where {T, N, P<:CategoricalArray} at ~/.julia/packages/CategoricalArrays/tJ8hD/src/subarray.jl:5
What am I doing wrong?
Thank you.