Combining categorical arrays
This example shows how to combine arrays of categorical variables.
Creating categorical arrays
Let's create an array of categorical values where the lunch break beverage preferences are stored for 25 students in a group. A.
Pkg.add("CategoricalArrays")
using Random, CategoricalArrays
Random.seed!(123)
A = rand(["молоко", "сок", "вода"], 25)
A = categorical(A, levels=["молоко", "сок", "вода"], ordered=true) # Передаем вектор меток чтобы задать их порядок
Summary statistics for the categorical array:
Pkg.add( "FreqTables" )
using FreqTables
freqtable(A)
Let's create another categorical array with the wishes of 28 students from the group B.
B = rand(["молоко", "сок", "вода"], 28) # Более сжатый синтаксис
B = categorical(B)
Summary statistics:
freqtable(B)
Combining categorical arrays
Let's combine the data from the classes A and B into one categorical array Group1.
Group1 = vcat(A, B)
Summary statistics:
freqtable(Group1)
Creating a categorical array with other categories
Creating a categorical array Group2, containing the wishes of 50 students with an additional drink option: * soda*.
Group2 = rand(["сок", "молоко", "газировка", "вода"], 50)
Group2 = categorical( Group2 )
Summary statistics:
freqtable(Group2)
Combining arrays with different categories
Combine the data from Group1 and Group2.
students = [Group1; Group2]
Summary statistics. When combining, the categories unique to the second array (soda) are added to the end of the list of categories in the first array (milk, water, juice, soda).
freqtable(students)
To change the order of categories in the categorical array, use the function levels!.
levels!(students, ["сок", "молоко", "вода", "газировка"])
levels(students)
Combining categorical arrays
To find the unique values of the categories present in Group1 and Group2, you can use the function union.
C = union(Group1, Group2)
Conclusion
All categorical arrays in this example were unordered. To combine ordered categorical arrays, they must have the same sets of categories, including their order.