Get Started

Arithmetic with R

In its most basic form, R can be used as a simple calculator. Consider the following arithmetic operators:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: ^
  • Modulo: %%

The last two might need some explaining:

  • The ^ operator raises the number to its left to the power of the number to its right: for example 3^2 is 9.
  • The modulo returns the remainder of the division of the number to the left by the number on its right, for example 5 modulo 3 or 5 %% 3 is 2.

With this knowledge, follow the instructions to complete the exercise.

This is a part of the course

“Introduction to R”

View Course

Exercise instructions

  • Type 2^5 in the editor to calculate 2 to the power 5.
  • Type 28 %% 6 to calculate 28 modulo 6.
  • Submit the answer and have a look at the R output in the console.
  • Note how the # symbol is used to add comments on the R code.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# An addition
5 + 5 

# A subtraction
5 - 5 

# A multiplication
3 * 5

 # A division
(5 + 5) / 2 

# Exponentiation


# Modulo

This exercise is part of the course

Introduction to R

BeginnerSkill Level
4.7+
595 reviews

Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.

Take your first steps with R. In this chapter, you will learn how to use the console as a calculator and how to assign variables. You will also get to know the basic data types in R. Let's get started.

Exercise 1: How it worksExercise 2: Arithmetic with R
Exercise 3: Variable assignmentExercise 4: Variable assignment (2)Exercise 5: Variable assignment (3)Exercise 6: Apples and orangesExercise 7: Basic data types in RExercise 8: What's that data type?

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free