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:
Syntax
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
x <- 5L
y <- 5
cat(class(x), class(y), "\n")
cat(is.integer(x), is.integer(y), "\n")
Login to try C/C++/Java/PHP code in the editor
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
- Writing
x <- 5and expecting an integer, when only5Lcreates an integer. - Adding
Lto a decimal like5.5L, which gives a warning and stays numeric. - Overflowing an integer such as
.Machine$integer.max + 1L, which givesNAwith a warning.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: