Convert Character to Factor in R (3 Examples)

 

This article shows how to convert characters to factors in the R programming language.

Table of contents:

Sound good? Let’s dig in…

 

Example 1: Convert Character Vector to Factor

This example shows how to convert a vector with character class to the factor class in the R programming language. Consider the following example vector:

vec <- c("A", "B", "A", "D", "C", "B")                     # Create example vector

We can check the class of our vector with the class function:

class(vec)                                                 # Check class of vector
# "character"

The RStudio console shows the data type of our vector: It’s a character. Now, we can use the as.factor function to convert this character string to the factor class:

vec_updated <- as.factor(vec)                              # Convert character vector to factor

Our updated vector is stored in the data object vec_updated. Again, we can use the class function to check the class of our updated vector:

class(vec_updated)
# "factor"

It’s a factor!

 

Example 2: Convert Character Column to Factor

We can also convert character variables (i.e. columns) of data frames to the factor type. Consider the following example data frame:

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

As you can see based on the output of the RStudio console, our example data frame contains of five rows and four columns, whereby column 2 and 4 are characters.

Let’s first duplicate this data frame for this example:

data2 <- data                                              # Replicate example data frame

Now, we can check the class of the column x2 by applying the class function to this column:

class(data2$x2)                                            # Check class of second column
# "character"

As you can see, our column has the character class. Now, we can apply the as.factor class to replace our character column with the corresponding factor:

data2$x2 <- as.factor(data2$x2)                            # Convert character column to factor

If we check the class again, we can see that the updated column is a factor:

class(data2$x2)                                            # Check class of second column
# "factor"

 

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

In Example 2, I explained how to convert one character variable to a factor in R. In this example, I’ll illustrate how to convert all character columns to factor in R.

Let’s duplicate our example data again:

data3 <- data                                              # Replicate example data frame

Now, we can use the str function to identify the classes of all our variables:

str(data3)                                                 # Check class of all columns
# 'data.frame':	5 obs. of  4 variables:
# $ x1: int  1 2 3 4 5
# $ x2: chr  "a" "b" "c" "d" ...
# $ x3: num  4 1 5 3 1
# $ x4: chr  "Male" "Female" "Male" "Male" ...

The previous output visualizes that columns 2 and 4 are characters. Now, we can use the as.data.frame function in combination with the unclass function to convert all character columns to factor in R:

data3 <- as.data.frame(unclass(data3))                     # Convert all columns to factor

If we apply the str function again, we can see the change:

str(data3)                                                 # Check class of all columns
# 'data.frame':	5 obs. of  4 variables:
# $ x1: int  1 2 3 4 5
# $ x2: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
# $ x3: num  4 1 5 3 1
# $ x4: Factor w/ 2 levels "Female","Male": 2 1 2 2 1

Columns 2 and 4 were converted to factors.

 

Video & Further Resources

In case you need more info on the examples of this article, I can recommend to have a look at the following video of my YouTube channel. I explain the contents of this article in the video:

 

The YouTube video will be added soon.

 

Additionally, you might have a look at the other articles of this website. Some articles are listed here:

 

In this R article you learned how to change vectors and columns from character string to factor. If you have any additional questions, don’t hesitate to let me know in the comments section.

 



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.

Menu
Top