unclass Function in R (2 Examples)

 

In this R tutorial you’ll learn how to apply the unclass() function.

The post is structured as follows:

Let’s just jump right in…

 

Creating Example Data

We’ll use the following data as a basis for this tutorial:

my_fac <- factor(c("A", "B", "A", "A", "C"))    # Create example factor
my_fac                                          # Print example factor
# [1] A B A A C
# Levels: A B C

As you can see based on the previously shown output of the RStudio console, our example data is a factor vector containing the three different factor levels A, B, and C.

 

Example 1: Convert Factor to Integer Using unclass() Function

The following R programming syntax shows how to use the unclass function to transform a factor vector to an integer.

Consider the R code below:

my_fac_new1 <- unclass(my_fac)                  # Convert factor to integer
my_fac_new1                                     # Print output
# [1] 1 2 1 1 3
# attr(,"levels")
# [1] "A" "B" "C"

As you can see, we have created a new data object called my_fac_new1, which contains integer values that correspond to the different levels of our input factor.

Let’s check the data class of this new data object:

class(my_fac_new1)                              # Check class of updated data
# [1] "integer"

As you can see, our new data object has the integer class.

 

Example 2: Change Group Names of Factor Using unclass() Function

This example shows how to use the unclass command to change the groups/categories of a factor object.

For this, we first have to create a new vector that contains the new group names:

new_names <- c("gr1", "gr2", "gr3")             # Specify new group names
new_names                                       # Print new group names
# [1] "gr1" "gr2" "gr3"

Next, we can use the unclass function to switch the categories in our factor to the new names:

my_fac_new2 <- new_names[unclass(my_fac)]       # Change factor levels
my_fac_new2                                     # Print new data
# [1] "gr1" "gr2" "gr1" "gr1" "gr3"

The factor level A was renamed to “gr1”, B was renamed to “gr2”, and C was renamed to “gr3”.

Note that the previously created data object my_fac_new2 has the character class. You might convert this vector back to the factor class, though.

 

Video, Further Resources & Summary

Do you need further explanations on the R programming code of this article? Then you might have a look at the following video on my YouTube channel. In the video, I’m explaining the topics of this tutorial.

 

The YouTube video will be added soon.

 

In addition, you may have a look at the other tutorials on this website:

 

To summarize: You have learned in this tutorial how to use the unclass() function in R programming. In case you have any further questions, let me know in the comments section below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top