How can character data be converted to factor data in R? Can you provide some examples?

Character data refers to a type of data in R that contains text or string values. On the other hand, factor data in R is a type of categorical data that represents different levels or categories. In order to convert character data into factor data, the function “as.factor()” can be used. This function takes in a vector of character data and converts it into a factor variable with levels based on the unique values in the vector.

For example, if we have a vector of character data representing different car brands: “Toyota”, “Ford”, “Honda”, “Toyota”, “Chevrolet”, the “as.factor()” function will convert it into a factor variable with levels “Toyota”, “Ford”, “Honda”, “Chevrolet”. This allows for easier manipulation and analysis of the data, as categorical variables are commonly used in statistical models and graphs.

Another example could be converting a vector of months in character form: “January”, “February”, “March”, “April”, “May”, “June” into a factor variable with levels “January”, “February”, “March”, “April”, “May”, “June”. This conversion allows for easier comparison and grouping of data based on the different months.

In summary, the “as.factor()” function is an important tool in R for converting character data into factor data, making it easier to work with and analyze categorical variables.

Convert Character to Factor in R (With Examples)


We can use the following syntax to convert a character vector to a factor vector in R:

factor_vector <- as.factor(character_vector)

This tutorial provides several examples of how to use this function in practice.

Example 1: Convert a Vector from Character to Factor

The following code shows how to convert a character vector to a factor vector:

#create character vector
character_vector <- c('First', 'Second', 'Third')

#convert character vector to factor vector
factor_vector <- as.factor(character_vector)

#view factor vector
factor_vector

[1] First  Second Third 
Levels: First Second Third

#confirm class of factor vector
class(factor_vector)

[1] "factor"

Example 2: Convert a Column from Character to Factor

The following code shows how to convert a specific column in a data frame from character to factor:

#create data frame
df <- data.frame(a = c('12', '14', '19', '22', '26'),
                 b = c(28, 34, 35, 36, 40))

#convert column 'a' from character to factor
df$a <- as.factor(df$a)

#view new data frame
df

       a  b
1  First 28
2 Second 34
3  Third 40

#confirm class of factor vector
class(df$a)

[1] "factor"

Example 3: Convert Several Columns from Character to Factor

The following code shows how to convert all character columns in a data frame from character to factor:

#create data frame
df <- data.frame(a = c('12', '14', '19', '22', '26'),
                 b = c('28', '34', '35', '36', '40'),
                 c = as.factor(c(1, 2, 3, 4, 5)),
                 d = c(45, 56, 54, 57, 59))

#display classes of each column
sapply(df, class)

          a           b           c           d 
"character" "character"    "factor"   "numeric" 

#convert all character columns to factor
df <- as.data.frame(unclass(df), stringsAsFactors = TRUE)

#display classes of each column
sapply(df, class)

        a         b         c         d 
 "factor"  "factor"  "factor" "numeric"

This code made the following changes to the data frame columns:

  • Column a: From character to factor
  • Column b: From character to factor
  • Column c: Unchanged (since it was already a factor)
  • Column d: Unchanged (since it was numeric)

By using the and functions, we were able to convert only the character columns to factor columns and leave all other columns unchanged.

Additional Resources

x