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)

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        


[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    
# Apply type.convert function
x2_convert <- type.convert(x2)  
# Class of converted data       


[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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :