Merging categorical arrays
This example shows how to merge arrays of categorical variables.
Creating categorical arrays
Let's create an array of categorical values that stores lunchtime drink requests for 25 students in a group A
.
using Random, CategoricalArrays
Random.seed!(123)
A = rand(1:3, 25)
A = categorical(A, levels=[1,2,3], labels=["молоко", "сок", "вода"]) # Назначаем метки значениям вручную
Summary statistics on 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)
Merging categorical arrays
Let's merge data from A
and B
classes into one categorical array Group1
.
Group1 = [A; B]
Summary statistics:
freqtable(Group1)
Creating a categorical array with other categories
Let's create a categorical array Group2
, containing the wishes of 50 students with an additional drink option: gazarivka.
Group2 = rand(["сок", "молоко", "газировка", "вода"], 50)
Group2 = categorical( Group2 )
Summary statistics:
freqtable(Group2)
Combining arrays with different categories
Let's combine data from Group1
and Group2
.
students = [Group1; Group2]
Summary statistics. When merging, the categories unique to the second array (gazarovka) are added to the end of the list of categories of the first array (milk, water, juice, soda).
freqtable(students)
To change the order of categories in a categorical array, we use the function levels!
.
levels!(students, ["сок", "молоко", "вода", "газировка"])
levels(students)
Merging categorical arrays
To find unique values of categories present in Group1
and Group2
, you can use the function union
.
C = union(Group1, Group2)
Conclusion
All the categorical arrays in this example were unordered. To merge ordered categorical arrays, they must have the same sets of categories, including their order.