How to Convert Factor to Character in R?
Last Updated :
19 Dec, 2021
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"
Similar Reads
How to Convert Character to Factor in R?
The as.factor() method in R Programming Language is used to convert the character vector to factor class. Converting Character Vector To Factor Syntax: as.factor(char-vec) where char-vec is the character vector The class indicative of the data type of the vector can be obtained using the class() me
2 min read
How to Convert Character to Numeric in R?
In this article, we will discuss how to convert characters to numeric in R Programming Language. We can convert to numeric by using as.numeric() function. Syntax: as.numeric(character) where, character is an character vector Example: R # create a vector with 5 characters data = c('1', '2', '3', '4',
1 min read
How to convert dataframe columns from factors to characters in R?
In this article, we will discuss how to convert dataframe columns from factors to characters in R Programming Language. A dataframe can have different types of columns stacked together to form a tubular structure. Easy modification of the columns' data as well as conversion between data types can be
5 min read
How to convert factor levels to list in R ?
In this article, we are going to discuss how to convert the factor levels to list data structure in R Programming Language. We can get the levels of the vector using factor() function Syntax: factor(vector) Return type: vector elements with levels. If we want to get only levels, Then we can use leve
2 min read
How to convert DataFrame column from Character to Numeric in R ?
In this article, we will discuss how to convert DataFrame column from Character to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type c
5 min read
How to convert table to dataframe in R?
In this article, we will discuss how to convert a given table to a dataframe in the R programming language. Functions Usedas.data.frame.matrix() will be taking the table as its parameter and will return the dataframe back to the user. Syntax: as.data.frame.matrix(x) Parameter: x: name of the table w
1 min read
Convert Character Matrix to Numeric Matrix in R
In this article, we are going to see how to convert a given character matrix to numeric in R Programming Language. Converting the Character Matrix to Numeric Matrix we will use as.numeric() & matrix() Functions. Functions Usedas.numeric() function: This function is used to convert a given column
3 min read
How to Convert String to Datetime in R?
In this article, we will discuss how to convert String to Datetime in R Programming Language. We can convert string to DateTime by using the POSIXct function Syntax: as.POSIXct(string, format="%Y-%m-%d %H:%M:%S", tz="UTC") where string is the input stringformat represents the datetime formattz speci
1 min read
Convert Character String to Variable Name in R
In this article we will discuss how to convert a character string to the variable name in the R programming language i.e. we are going to assign the character string to the variable as the variable name Method 1: Using assign() function We can assign character string to variable name by using assign
2 min read
Concatenate Vector of Character Strings in R
In this article, we will discuss how to concatenate the strings present in two or more vectors in R Programming Language. Discussed below are various methods of doing so. Method 1: Using paste() paste() function is used to combine strings present in vectors passed to it  an argument. Syntax: paste(
4 min read