Convert a Character Object to Integer in R Programming – as.integer() Function

as.integer() function in R Language is used to convert a character object to integer object.

Syntax: as.integer(x)

Parameters:
x: Character Object

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to convert a character object
# to an integer object
  
# Calling as.integer() function
as.integer("4")
as.integer("1.6")
as.integer("-3.2")
as.integer("0x400")

chevron_right


Output:

[1] 4
[1] 1
[1] -3
[1] 1024

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to convert a character object
# to an integer object
  
# Creating a vector
x <- c(1, 2, 3, 4, 5)
  
# Check if the vector is an integer
is.integer(x)
  
# Convert to integer
x1 <- as.integer(x)
x1
  
# Calling is.integer() function
is.integer(x1)

chevron_right


Output:

[1] FALSE
[1] 1 2 3 4 5
[1] TRUE
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.