Categorical Representation of Data in Julia - GeeksforGeeks

15 Sep, 2021
Julia is a high performance, dynamic programming language that is easy to learn as it is a high-level language. But sometimes, when dealing with data in programming languages like Julia, we encounter structures or representations with a small number of levels as represented below.
Julia
# Creating an array of stingsa = ["Geeks", "For", "Geeks", "Useful", "For", "Everybody"]
As you can see, the elements of the array are simply categorized as full strings. 
By changing the array type to CategoricalArray type we can represent the elements better to make things easier in the future for some tasks. The CategoricalArray type represents the strings as indices in a number of levels. 
Julia
# Creating an array of the # CategoricalArray type of the array 'a'cat = CategoricalArray(a)
In the example mentioned above, 232 levels are represented (UInt32).
CategoricalArray type can also classify a missing value as shown below:
Julia
# Creating array of CategoricalArray # type with some missing valuescat = CategoricalArray(["Geeks", "For", "Geeks",                         missing, missing, "Everybody"])
CategoricalArray type allows us to know the levels which are valid as there are repeated data, by using the levels() function where the argument to be passed is the array. 
Julia
# Determining levels of the arraylevels(cat)
We can change the placement or order of the levels by using the levels!() function, as it might be useful later on.
Julia
# Changing the order of the levels displayedlevels!(cat, ["Geeks", "For", "Everybody"]);levels(cat)
And we can sort the array according to the changed order of the levels.
Julia
# Sorting array according to the levelssort(cat)
The CategoricalArray type can have 232 levels as shown in the description of the array in the outputs. If these many levels are not required we decrease them by using the compress() function. The following example shows the decrease of the levels to 28 levels.
Julia
# Decreasing the number of levels for the array 'cat'cat = compress(cat)
We can directly use the categorical function instead of using CategoryArrays which allows us to apply a keyword argument like the compress keyword which when set to ‘true’, implicates implementation of that keyword on the elements.
Julia
# Creating a categorical array and # applying the compress functioncat2 = categorical(["Geeks", "For", "Geeks"], compress = true)
 In the same way, we have implemented the compress keyword, the ordered keyword can be implemented by equating it to ‘true’, which gives an order to the levels of the array.
Julia
# Creating an ordered categorical arraycat3 = categorical(["Geeks", "For", "Geeks"], ordered=true)
We can check the levels of arrays for order and when it is not an ordered array, it produces an error as shown below.
Julia
# Testing levels of unordered arraycat2[1] < cat2[2]
When the array is ordered, it results in either true or false based on the order of the levels.
Julia
# Testing levels of the ordered arraycat3[1] < cat3[2]
We can check whether if an array is ordered with the isordered() function.
Julia
# Checking if array is orderedisordered(cat2)
We can change an unordered array to ordered and vice-versa by using the ordered!() function.
Julia
# Changing unordered array to an ordered arrayordered!(cat2, true)
Now that we have ordered the array, we can test it.
Julia
# Testing levels of the arraycat2[1] < cat2[2]
We can implement the categorical function on one or more columns of a Dataframe by using the categorical!() function in which the first argument is the DataFrame and the second argument can be columns of the DataFrame we want to apply on and some keyword function.
Julia
# Creating a DataFrame with String elementsusing DataFrames  df = DataFrame(A = ["A", "A", "A", "B", "B", "C"],               B = ["D", "E", "E", "F", "G", "G"])
We can change the type of a specific column of the DataFrame to categorical type. 
Julia
# Changing the column A to categoricalcategorical!(df, :A)
If we don’t specify the column, the columns with an AbstractString type will change to categorical. By equating compress keyword function to true we can apply the function on all of the columns.
Julia
# Changing columns to categorical typecategorical!(df, compress=true)
We can check the types of the columns of the DataFrame with eltype() function.
Julia
# Displaying column typeseltype.(eachcol(df))
abhishek0719kadiyan
Picked
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Get array dimensions and size of a dimension in Julia - size() Method
Searching in Array for a given element in Julia
Find maximum element along with its index in Julia - findmax() Method
Get number of elements of array in Julia - length() Method
Getting the maximum value from a list in Julia - max() Method
Working with Excel Files in Julia
Exception handling in Julia
Working with Date and Time in Julia
Decision Making in Julia (if, if-else, Nested-if, if-elseif-else ladder)
Getting the absolute value of a number in Julia - abs() Method