We can use the following syntax to convert a character vector to a factor vector in R:
factor_vector 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 #convert character vector to factor vector
factor_vector 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 frame(a = c('12', '14', '19', '22', '26'),
b = c(28, 34, 35, 36, 40))
#convert column 'a' from character to factor
df$a 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 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 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 apply() and sapply() functions, we were able to convert only the character columns to factor columns and leave all other columns unchanged.
Additional Resources
How to Convert Factor to Character in R
How to Convert Character to Numeric in R
How to Convert Strings to Dates in R