Open In App

How to Convert Factor to Character in R?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to convert the Factor Class to the character Class in the R Programming Language.

Method 1: Convert a Single Factor Vector to Character Vector

To convert a single factor vector to a character vector we use the as.character() function of the R Language and pass the required factor vector as an argument. 

Syntax:

 character_vector <- as.character( Factor_vector )

Example:

Here, in this example, we have converted a factor vector to a character vector by using as.character() function.

R




# create factor vector
Input <- factor(c("Geeks", "for", "Geek"))
  
# print input factor vector and its class
print("Input vector:")
Input
print("Input Data Class:")
class(Input)
  
# Convert factor vector to character vector
Output <- as.character(Input)
  
# print Output character vector and its class
print("Output vector:")
Output
print("Output Data Class:")
class(Output)


Output:

Input vector:
Geeks for   Geek 
Input Data Class:
factor
Output vector:
 "Geeks" "for"   "Geek"
Output Data Class:
character

Method 2: Convert Factor vector columns to Character vector columns in a data frame

To convert a known factor vector to a character vector we create a duplicate copy of the input data frame. Then use the as.character() function of the R Language and pass the required factor vector column of the data frame as an argument.

Syntax:

Output$factor_vector_column <- as.character( Output$factor_vector_column )

Example:

Here, in this example, we have converted a factor vector column to a character vector column by using as.character() function.

R




# create factor vector
x <- factor(c("Geeks", "for", "Geek"))
y <- c(1,2,3)
Input <- data.frame( x, y )
  
# print input factor vector and its data Class
print("Input vector:")
Input
print("Input Data Class:")
class(Input$x)
  
# Convert factor vector to character vector
Output <- Input
Output$x <- as.character(Input$x)
  
# print Output character vector and its Class
print("Output vector:")
Output
print("Output Data Class:")
class(Output$x)


Output:

Input vector:
      x y
1 Geeks 1
2   for 2
3  Geek 3
Input Data Class:
factor
Output vector:
      x y
1 Geeks 1
2   for 2
3  Geek 3
Output Data Class:
character

Method 3: Convert Factor vector columns to Character vector columns in a data frame where Columns are unknown

To convert a known factor vector to a character vector we create a duplicate copy of the input data frame. Then use is.factor() conditional statement to find the columns that have data class factor. Then use the as.character() function of the R Language and pass the identified factor vector column of the data frame as an argument.

Syntax:

factor_columns <- sapply(Output, is.factor)  
Output[factor_columns] <- lapply(Output[factor_columns], as.character)

Example:

Here, in this example, we have converted all factor vector columns to character vector columns by using as.character() function identified by is.factor() conditional statement.

R




# create Input dataframe
x <- factor(c("Geeks", "for", "Geek"))
y <- c(1,2,3)
z <- factor(c("Complete", "Interview", "Preparation"))
Input <- data.frame( x, y, z )
  
# print input data frame and its data Class
print("Input dataframe:")
Input
print("Input Data Class:")
sapply(Input, class) 
  
#Convert factor vector to character vector
Output <- Input                                          
factor_columns <- sapply(Output, is.factor)  
Output[factor_columns] <- lapply(Output[factor_columns], as.character)
  
# print Output character vector and its Class
print("Output dataframe:")
Output
print("Output Data Class:")
sapply(Output, class)


Output:

Input dataframe:
      x y           z
1 Geeks 1    Complete
2   for 2   Interview
3  Geek 3 Preparation
Input Data Class:
        x         y         z  
"factor" "numeric"  "factor"  
Output dataframe:      
      x y           z
1 Geeks 1    Complete
2   for 2   Interview
3  Geek 3 Preparation
Output Data Class:
          x           y           z  
"character"   "numeric" "character"  

Method 4: Convert all columns of the Data frame into character

To convert all columns of the data frame into the character we use apply() function with as.character parameter. The lapply() function applies the given function to the provided data frame.

Syntax:

dataframe <- lapply(dataframe, as.character)

Example:

Here, in this example, we have converted all columns of the data frame to character vector columns by using as.character function using lapply() function.

R




# create Input dataframe
x <- c("Geeks", "for", "Geek")
y <- c(1,2,3)
z <- factor(c("Complete", "Interview", "Preparation"))
df <- data.frame( x, y, z )
  
# print input data frame and its data Class
print("Input dataframe:")
df
print("Input Data Class:")
sapply(df, class) 
  
#Convert all columns of data frame to character vector
df <- lapply(df, as.character)
  
# print Output character vector and its Class
print("Output dataframe:")
df
print("Output Data Class:")
sapply(df, class)


Output:

Input dataframe:
      x y           z
1 Geeks 1    Complete
2   for 2   Interview
3  Geek 3 Preparation
Input Data Class:
          x           y           z  
"character"   "numeric"    "factor"  
Output dataframe:      
$x
[1] "Geeks" "for"   "Geek"  
$y
[1] "1" "2" "3"
$z
[1] "Complete"    "Interview"   "Preparation"
Output Data Class:
          x           y           z  
"character" "character" "character"  


Last Updated : 19 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads