How to Convert Factor to Character in R? - GeeksforGeeks

19 Dec, 2021
In this article, we will discuss how to convert the Factor Class to the character Class in the R Programming Language.
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 vectorInput <- factor(c("Geeks", "for", "Geek"))  # print input factor vector and its classprint("Input vector:")Inputprint("Input Data Class:")class(Input)  # Convert factor vector to character vectorOutput <- as.character(Input)  # print Output character vector and its classprint("Output vector:")Outputprint("Output Data Class:")class(Output)
Output:
Input vector:
Geeks for   Geek 
Input Data Class:
factor
Output vector:
 "Geeks" "for"   "Geek"
Output Data Class:
character
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 vectorx <- factor(c("Geeks", "for", "Geek"))y <- c(1,2,3)Input <- data.frame( x, y )  # print input factor vector and its data Classprint("Input vector:")Inputprint("Input Data Class:")class(Input$x)  # Convert factor vector to character vectorOutput <- InputOutput$x <- as.character(Input$x)  # print Output character vector and its Classprint("Output vector:")Outputprint("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
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 dataframex <- 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 Classprint("Input dataframe:")Inputprint("Input Data Class:")sapply(Input, class)   #Convert factor vector to character vectorOutput <- Input                                          factor_columns <- sapply(Output, is.factor)  Output[factor_columns] <- lapply(Output[factor_columns], as.character)  # print Output character vector and its Classprint("Output dataframe:")Outputprint("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"  
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 dataframex <- 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 Classprint("Input dataframe:")dfprint("Input Data Class:")sapply(df, class)   #Convert all columns of data frame to character vectordf <- lapply(df, as.character)  # print Output character vector and its Classprint("Output dataframe:")dfprint("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"  
Picked
R Factor-Programs
R-Factors
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R