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

R length(): Vector, List, Matrix, Array, Data Frame, String

Before executing an operation on an object, it is advisable to check its length, as…

15 hours ago

How to Round Numbers in R

Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…

2 days ago

Adding Single or Multiple Columns to Data Frame in R

Whether you want to add new data to your existing datasets or create new variables…

4 days ago

sqrt() Function: Calculate Square Root in R

The square root of a number is a value that is multiplied by itself, giving…

5 days ago

How to Remove Duplicate Rows from DataFrame in R

Duplicate rows refer to all the values across all columns that are the same in…

6 days ago

How to Remove NA From Vector in R

A vector is a data structure that holds the same type of data. When working…

1 week ago