Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
r
communitycreator

What is the as.integer() function in R?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The as.integer() function in R transforms a character object into an integer object.

Syntax

as.integer(x)

Parameter value

The as.integer() function takes a single mandatory parameter, x, representing the character object passed.

Return value

The as.integer() function returns an integer object.

Example 1

# calling the as.integer() function on character objects
as.integer("1")
as.integer("1.5")
as.integer("10")
as.integer("-10.5")
as.integer("0x290")
Implementing the as.integer() function

Code explanation

We call the as.integer() function on some given character objects in the code above. The function helps return their values as integer objects.

Example 2

# creating a vector
myvector <- c(1, 2, 3, 4, 5)
# converting the vector to integer
myinteger <- as.integer(myvector)
myinteger
# to check if it is an integer
is.integer(myinteger)
Implementing the as.integer() function on a vector object

Code explanation

  • Line 2: We create a vector object myvector.
  • Line 5: We implement the as.integer() function on the vector object myvector. We assign the result to another variable, myinteger.
  • Line 6: We print the variable myinteger.
  • Line 9: To confirm if the variable myinteger is an integer object, we make use of the is.integer() function.

RELATED TAGS

function
r
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring