Skip to content
Related Articles

Related Articles

Improve Article
Convert a Character Object to Integer in R Programming – as.integer() Function
  • Last Updated : 16 Jun, 2020

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:




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

Output:

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

Example 2:




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

Output:

[1] FALSE
[1] 1 2 3 4 5
[1] TRUE
My Personal Notes arrow_drop_up
Recommended Articles
Page :