Convert Factor to Character Class in R (3 Examples) | Change Vector & Data Frame Column

 

In this tutorial, I’ll show how to change vectors and data frame columns from factor to character class in the R programming language.

The content of the tutorial is structured as follows:

Let’s just jump right in:

 

Example 1: Convert Factor Vector to Character

The following R syntax illustrates how to convert a vector object (or array) from the factor class to character in R.

First, we have to create an example factor vector:

x_fac <- factor(c("AA", "XXX", "Y", "HI", "XXX", "YAY"))            # Create factor vector
x_fac                                                               # Print factor vector
# [1] AA  XXX Y   HI  XXX YAY
# Levels: AA HI XXX Y YAY

Let’s check the data type of our vector using the class function:

class(x_fac)                                                        # Check class
# [1] "factor"

The RStudio console returns the data type: Our vector has the factor class.

Now, we can use the as.character function to convert this factor to the character class:

x_char <- as.character(x_fac)                                       # Apply as.character function
x_char                                                              # Print new vector
# [1] "AA"  "XXX" "Y"   "HI"  "XXX" "YAY"

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

class(x_char)                                                       # Check class
# [1] "character"

It’s a character string!

 

Example 2: Convert One Data Frame Column from Factor to Character

This Example explains how to convert a single variable of a data frame from factor to character. First, we have to create some example data:

data_fac <- data.frame(x1 = letters[1:5],                           # Create example data
                       x2 = 1:5,
                       x3 = "XXX")
data_fac                                                            # Print example data
#   x1 x2  x3
# 1  a  1 XXX
# 2  b  2 XXX
# 3  c  3 XXX
# 4  d  4 XXX
# 5  e  5 XXX

We can use the sapply and the class functions to check the classes of all variables of our data frame:

sapply(data_fac, class)                                             # Check class of all variables
#       x1        x2        x3 
# "factor" "integer"  "factor"

The first and third columns are factors; the second column is an integer.

Now, let’s convert only the first variable from factor to character:

data_char1 <- data_fac                                              # Duplicate data
data_char1$x1 <- as.character(data_char1$x1)                        # Convert one column to character

Let’s have a look at the classes of our data frame columns again:

sapply(data_char1, class)                                           # Check class of all variables
#          x1          x2          x3 
# "character"   "integer"    "factor"

As you can see, the first variable was changed from factor to character.

 

Example 3: Convert All Factor Columns of Data Frame from Factor to Character

In case you are working with large data sets, you may be interested to convert all factor variables of your data frame to the character class. This Example explains how to do that using the sapply, as.character, and lapply functions:

data_char2 <- data_fac                                              # Duplicate data
fac_cols <- sapply(data_char2, is.factor)                           # Identify all factor columns
data_char2[fac_cols] <- lapply(data_char2[fac_cols], as.character)  # Convert all factors to characters

Let’s check the classes of all of our data frame columns:

sapply(data_char2, class)                                           # Check class of all variables
#          x1          x2          x3 
# "character"   "integer" "character"

Both factor columns were converted to the character class.

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. I show the R code of this tutorial in the video:

 

 

Additionally, you may want to have a look at some of the related articles on my homepage. I have released several other tutorials about topics such as factors, data conversion, and numeric values:

 

To summarize: This article explained how to switch from factor labels to character string in the R programming language. If you have any additional questions, please let me know in the comments.

 

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. Required fields are marked *

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

Top