Hands-on Data Science
My learning DE

Lesson8 min

R as a calculator

Your first contact with R: calculating, using brackets, dividing into whole numbers and writing comments, right in the browser.

Topics:Programming with R

What you will learn

This lesson introduces R from its simplest side, as a calculator in your browser, and afterwards you can:

  • calculate with +, -, *, / and ^, respecting operator precedence and brackets
  • write decimal numbers in R with a point rather than a comma
  • divide without a remainder using %/% and get the remainder with %%, for instance to assign an age to its decade
  • write comments after # that explain your code

R is a programming language made for working with data. Before data come in, you get to know R from its simplest side: as a calculator. Every code box on this page is a small editor. You can change the code and start it with Run; R runs in your browser, so there is nothing to install. On the first click, loading takes a moment.

Calculating

R computes what you write and shows the result straight away:

3 + 4
19.90 - 2.50
6 * 7
1 / 3

You write decimals with a point, as almost everywhere in programming. You can ignore the [1] in front of each result for now; it counts the values of an output and only becomes useful with long outputs.

Powers and brackets

^ raises to a power. Multiplication comes before addition, and brackets change the order, just as at school:

2^10
2 + 3 * 4
(2 + 3) * 4

Dividing into whole numbers

Two operators come up again and again when analysing data. %/% divides without a remainder, %% returns only the remainder:

17 %/% 5
17 %% 5

This lets you assign an age to its decade, for example: 47 %/% 10 * 10 gives 40.

Comments

R ignores everything after a #. That is how you write notes into your code that explain what it does:

# price of a litre of milk with 7 percent VAT
1.09 * 1.07

Comments are for people, including yourself in a few weeks.

Key points

  • R calculates with +, -, *, / and ^, multiplication before addition, brackets first.
  • %/% divides without a remainder, %% gives the remainder.
  • Decimals with a point, comments after #.
Back

Builds on this

Details
Level
Beginner
Modules
Empirical Research
In learning paths
Getting started with R
Code
2735
Updated
1 November 2026
Status
published

    A four-digit code takes you straight to the item.