Numeric Type
Numeric is R's usual kind of number, able to hold decimals as well as whole values. It is the default whenever you type a number.
In this page:
Syntax
x <- 3.14
class(x) # "numeric"
Numeric Type
numeric is R's default type for any number, storing values as double-precision floating point regardless of whether they look like whole numbers.
Most arithmetic in R produces numeric results. Use class() or typeof() to check a value's actual type.
Note:
Even
x <- 5 (no decimal point) creates a numeric (double), not an integer, unless you explicitly use 5L.Example: Numeric Type
x <- 5
y <- 5.5
cat(class(x), class(y), "\n")
cat(x + 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
- Assuming
5is an integer, when R stores it as a double soclass(5)is"numeric". - Comparing decimals with
==, as in0.1 + 0.2 == 0.3, which isFALSEbecause of floating-point rounding; useall.equal(). - Expecting integer division from
/, when5 / 2is2.5and you need%/%for2.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: