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

How to Check If File and Folder Already Exists in R

Whether you are reading or writing files via programs in the file system, it is…

1 day ago

How to Check Data type of a Variable in R

When it comes to checking the data type of a variable, it depends on what…

2 days ago

Mastering grepl() Function in R

The grepl() function (stands for "grep logical") in R searches for patterns within each element…

3 days ago

zip(), unzip() and tar(), untar() Functions in R

The zip() function creates a new zip archive file. You must ensure that the zip tool…

4 days ago

How to Create Directory and File If It doesn’t Exist in R

When working with file systems, checking the directory or file existence is always better before…

5 days ago

How to Create a Grouped Boxplot in R

To create a grouped boxplot in R, we can use the ggplot2 library's aes() and…

7 days ago