๐ŸŽ“ A1Learner
๐Ÿ”
0 of 40 lessons completed

R Numbers

A number holds a whole value or a decimal value. R can add, subtract, multiply and divide numbers.

Whole and decimal numbers

x holds a whole number and y holds a decimal number. The last print shows the sum of the two numbers.

x <- 42
y <- 3.5
print(x)
print(y)
print(x + y)

Whole division and remainder

a holds the number 7. The prints show a plain division and then its whole part and remainder.

a <- 7
print(a / 2)
print(a %/% 2)
print(a %% 2)
print(-7 %/% 2)

Powers and display

The first print raises 2 to the power 10. The next prints show 1000 in a short form and a division result with limited digits.

print(2 ^ 10)
print(1e3)
print(2 / 3)

Example

price <- 20
count <- 4
print(price / count)
print(price * count)
print(1 / 0)
print(0 / 0)

Key points

  • A whole number is written with no decimal point.
  • A decimal number is written with a decimal point.
  • + adds numbers and - subtracts numbers.
  • * multiplies numbers and / divides numbers.
  • Division with / always gives a decimal result.
  • %/% gives the whole part of a division and %% gives the remainder.
  • ^ raises a number to a power.

In this example, price is divided by count and then multiplied by count. The last two prints show the special results Inf and NaN.

Note

R shows a number with up to 7 significant digits.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท rdrr.io R Snippets

Exercise

What does print(7 %% 2) show?