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() method. Upon conversion, the data type is returned as a factor.
class(fac-vec)
where char-vec is the character vector
Example:
R
# declaring a character vector str_vec < - c ( "Geeks" , "For" , "Geeks" , "Programming" , "Coding" ) print ( "Original String" ) print (str_vec) # getting the class of vector class (str_vec) str_mod < - as.factor (str_vec) print ( "Modified String" ) print (str_mod) # getting the class of vector class (str_mod) |
Output
[1] "Original String" [1] "Geeks" "For" "Geeks" "Programming" "Coding" [1] "character" [1] "Modified String" [1] Geeks For Geeks Programming Coding Levels: Coding For Geeks Programming [1] "factor"
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.
Contact Us