Convert type of data object in R Programming – type.convert() Function
Last Updated :
26 May, 2020
type.convert()
function in R Language is used to compute the data type of a particular data object.
It can convert data object to logical, integer, numeric, or factor.
Syntax: type.convert(x)
Parameter:
x: It can be a vector matrix or an array
Example 1: Apply type.convert to vector of numbers
x1 < - c( "1" , "2" , "3" , "4" , "5" , "6" , "7" )
x1_convert < - type .convert(x1)
class (x1_convert)
|
Output:
[1] "integer"
Here in the above code, we can see that before type.convert()
function all the numbers are stored in it, but still its a character vector. And after the function it became “Integer”.
Example 2: Type conversion of a vector with both characters and integers.
x1 < - c( "1" , "2" , "3" , "4" , "5" , "6" , "7" )
x2 < - c(x1, "AAA" )
class (x2)
x2_convert < - type .convert(x2)
class (x2_convert)
|
Output:
[1] "character"
[1] "factor"
Here, in the above code there were some characters and some integers after using type.convert() function. So, the output comes to be a factor.
Share your thoughts in the comments
Please Login to comment...