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

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

2 days ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

2 weeks ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

3 weeks ago

Printing an Output of a Program in R

When working with R in an interactive mode, you don't need to use any functions…

4 weeks ago

How to Calculate Variance in R

To calculate the sample variance (measurement of spreading) in R, you should use the built-in…

1 month ago

tryCatch() Function in R

The tryCatch() function acts as a mechanism for handling errors and other conditions (like warnings…

1 month ago