0
0
R Programmingprogramming~5 mins

Integer type in R Programming

Choose your learning style9 modes available
Introduction

Integers are whole numbers without decimals. They help us count or measure things exactly.

When you need to count items like apples or books.
When you want to store ages or years.
When you want to use numbers without fractions in calculations.
When you want to save memory by using whole numbers only.
Syntax
R Programming
x <- 5L

The L after the number tells R this is an integer.

Without L, numbers are treated as double (decimal) by default.

Examples
This creates an integer 10 and checks its type.
R Programming
a <- 10L
class(a)
This creates a decimal number 3.5, which is not an integer.
R Programming
b <- 3.5
class(b)
This converts the number 7 to an integer type.
R Programming
c <- as.integer(7)
class(c)
Sample Program

This program creates an integer variable x with value 12, prints it, and then prints its type.

R Programming
x <- 12L
print(x)
print(class(x))
OutputSuccess
Important Notes

Adding L after a number is the easiest way to make it an integer in R.

Integers use less memory than decimal numbers.

Be careful: dividing integers can result in decimals.

Summary

Integers are whole numbers without decimals.

Use L after a number to make it an integer in R.

Integers are useful for counting and exact values.