← Back to R Course | Chapter 2: Variables & Types | Lesson 3 of 7

Integer Type

An integer is a whole number with no decimals, marked by an L after it. It is like counting whole apples, with no slices.

In this page:

  1. Integer Type
Syntax
r
x <- 5L
class(x)    # "integer"

Integer Type

An explicit integer is created with an L suffix, like 5L, distinct from the default numeric (double) type. Integers use less memory and are useful when a value is guaranteed to be a whole number, such as an index or count.

is.integer() checks whether a value is specifically of integer type.

Note: Without the L suffix, 5 is a double, not an integer, even though it looks like a whole number.

Example: Integer Type

markup
x <- 5L
y <- 5

cat(class(x), class(y), "\n")
cat(is.integer(x), is.integer(y), "\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing x <- 5 and expecting an integer, when only 5L creates an integer.
  2. Adding L to a decimal like 5.5L, which gives a warning and stays numeric.
  3. Overflowing an integer such as .Machine$integer.max + 1L, which gives NA with a warning.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.