Convert type of data object in R Programming – type.convert() Function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)                        
                                

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)          

chevron_right


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 Posts:


Check this out nerdboinet

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.