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.

Share
Published by
Krunal Lathiya

Recent Posts

Understanding the as.numeric() Function in R

Data occasionally comes in various formats, including numeric values stored as text (characters) or categories…

3 weeks ago

How to Append an Element to a List at Any Position in R

When you don't know the size of your data, you can use the "list" data…

4 weeks ago

How to Convert List to Numeric in R

To convert a list to a numeric value in R, you can combine the unlist()…

1 year ago

How to Use “lwd” in R

The lwd in R is used to specify the width of plot lines. It is…

1 year ago

R dirname() Function

The dirname() function in R is used to extract the path to the directory from…

1 year ago

R month.abb

The month.abb is a built-in R constant, a three-letter abbreviation for the English month names. Example 1:…

1 year ago