How to Convert Character to Factor in R

To convert a character to a factor in R, you can “use the as.factor() function.”

Syntax

as.factor(char)

Parameters

char: It is the character vector.

Example

# Create a character vector
fruits <- c("Apple", "Banana", "Cherry", "Apple", "Banana", "Dragonfruit")

# Convert it to a factor
fruits_factor <- factor(fruits)

# Display the factor
print(fruits_factor)

Output

[1] Apple Banana Cherry Apple Banana Dragonfruit

Levels: Apple Banana Cherry Dragonfruit

Ordered Factors

To make an ordered factor (useful for ordinal data), you can use the ordered argument:

# Create a character vector
fruits <- c("Apple", "Banana", "Cherry", "Apple", "Banana", "Dragonfruit")

# Convert it to a factor
fruits_factor <- factor(fruits, ordered = TRUE)

# Display the factor
print(fruits_factor)

Output

[1] Apple Banana Cherry Apple Banana Dragonfruit

Levels: Apple < Banana < Cherry < Dragonfruit

Specify Levels

You can specify the levels explicitly, which is useful when you have a known set of categories, even if they don’t all appear in the data:

# Create a character vector
fruits <- c("Apple", "Banana", "Cherry", "Apple", "Banana", "Dragonfruit")

fruits_levels <- factor(fruits, levels = c(
  "Apple", "Banana", "Cherry",
  "Dragonfruit", "Elderberry"
))

# Display the factor levels
print(fruits_levels)

Output

[1] Apple Banana Cherry Apple Banana Dragonfruit

Levels: Apple Banana Cherry Dragonfruit Elderberry

Converting DataFrame Column to Factor Column

In base R, you can convert a specific column to a factor using the factor() function and assignment.

# Create a sample data frame
my_data <- data.frame(name = c("Alice", "Bob", "Carol"),
  age = c(25, 30, 35),
  gender = c("Female", "Male", "Female"),
  stringsAsFactors = FALSE)

# Convert the 'gender' column to a factor
my_data$gender <- factor(my_data$gender)

# Display the data frame
print(my_data)
class(my_data$gender)

Output

Converting DataFrame Column to Factor Column

Convert All Character Columns of the Data Frame to Factor

In base R, you can use the lapply() function along with sapply() to identify character columns and convert them to factors.

# Create a sample data frame
my_data <- data.frame(name = c("Alice", "Bob", "Carol"),
  age = c(25, 30, 35),
  gender = c("Female", "Male", "Female"),
  stringsAsFactors = FALSE)

# Identify character columns
char_cols <- sapply(my_data, is.character)

# Convert all character columns to factors
my_data[char_cols] <- lapply(my_data[char_cols], factor)

# Display the data frame
print(my_data)

class(my_data$name)
class(my_data$gender)

Output

Convert All Character Columns of the Data Frame to Factor

Conclusion

The factor() and as.factor() functions are designed to convert character vectors or columns to factors. The two choices often depend on the context and specific use cases.

Beginners: If you’re new to R or performing a simple conversion without setting custom levels or ordering, as.factor() may be the more straightforward choice.

Advanced Users: If you need more control over the factor levels or deal with ordinal data, factor() offers additional arguments that provide greater flexibility.

Data Preprocessing: In data cleaning and preprocessing, either function can be used, but factor() is more versatile for setting specific levels or ordering, which can be particularly useful in statistical models.

Related posts

Factor to Character in R

Date to Character in R

Character Vector to Numeric in R

Leave a Comment