Skip to content
Related Articles

Related Articles

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




# R program to illustrate
# type.convert to vector of numbers
  
# create an example vector
x1 <- c("1", "2", "3", "4", "5", "6", "7")  
  
# Apply type.convert function
x1_convert <- type.convert(x1) 
      
# Class of converted data        
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.




# R Program to illustrate
# conversion of a vector with both characters and integers
  
# create an example vector
x1 <- c("1", "2", "3", "4", "5", "6", "7")  
  
# Create example data
x2 <- c(x1, "AAA")  
  
# Class of example data    
class(x2)                        
  
# Apply type.convert function
x2_convert <- type.convert(x2)  
  
# Class of converted data       
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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :