R Basic

How to Convert Double to Integer in R

To convert a double or float to an integer, you can use the “as.integer()” function.

Syntax

as.integer(x)

Parameters

x: It is the object.

Return value

It returns an integer object.

Visual representation

Example 1: Converting a float to an integer

r_float <- 11.21
print(r_float)
print(typeof(r_float))

r_int <- as.integer(r_float)
print(r_int)
print(typeof(r_int))

Output

[1] 11.21
[1] "double"
[1] 11
[1] "integer"

Example 2: Check if a variable is an integer

Use the is.integer() function to check if a variable is an integer.

r_float <- 11.21
r_int <- as.integer(r_float)

print(is.integer(r_int))

Output

[1] TRUE

That’s it.

Recent Posts

What is is.factor() Function in R

R consists of various data types, and "factor" is one of them. You can use…

10 mins ago

Efficiently Check If a Vector is Empty in R

The most efficient and idiomatic way to check if a vector is empty in R…

1 day ago

Checking If a Data Frame is Empty in R

What criteria are being evaluated to determine if a data frame is empty? There is…

2 days ago

How to Check If a List is Empty in R

What do we mean when we say an empty list? An empty list does not…

3 days ago

is.element() Function: Check Presence of Elements in R

Whether you want to do membership testing, filter data, identify missing values, check for duplicates,…

4 days ago

as.double() and is.double() Functions in R Language

Whether you want to perform calculations efficiently or derive accurate analysis, you need a double-precision…

1 week ago