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(["milk", "juice", "water"], 25)
A = categorical(A, levels=["milk", "juice", "water"], ordered=true) # Passing a vector of labels to set their order
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(["milk", "juice", "water"], 28) # More concise syntax
B = categorical(B)
Summary statistics:
freqtable(B)
Combining categorical arrays
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(["juice", "milk", "soda", "water"], 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, ["juice", "milk", "water", "soda"])
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.