To convert a double or float to an integer, you can use the “as.integer()” function.
as.integer(x)
x: It is the object.
It returns an integer object.
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"
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.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…
In R, you can use the dollar sign ($ operator) to access elements (columns) of…
The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…
When working with R in an interactive mode, you don't need to use any functions…
To calculate the sample variance (measurement of spreading) in R, you should use the built-in…
The tryCatch() function acts as a mechanism for handling errors and other conditions (like warnings…