The Basics of R and RStudio

Part 1: Simple Arithmetic

RStudio
R
Tutorial
Blog
Author

William Okech

Published

June 15, 2022

Introduction

This is the first in a series of blog posts looking at the basics of R and RStudio. These programs allow us to perform various basic and complex calculations.

To get started, first, we will open R or RStudio. In R, go to the console, and in RStudio, head to the console pane. Next, type in a basic arithmetic calculation such as “1 + 1” after the angle bracket (>) and hit “Enter.”

An example of a basic calculation:

1+1
[1] 2

The output will be observed next to the square bracket containing the number 1 ([1]).

Additionally, to include comments into the code block we use the hash (#) symbol. Anything written after the code block will be commented out and not run.

# A simple arithmetic calculation (which is not run because of the hash symbol)
1+1
[1] 2

Arithmetic operators available in R/RStudio

Various arithmetic operators (listed below) can be used in R/RStudio.

Arithmetic Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
** or ^ Exponentiation
%% Modulus (remainder after division)
%/% Integer division

Examples

Addition

10+30
[1] 40

Subtraction

30-24
[1] 6

Multiplication

20*4
[1] 80

Division

93/4
[1] 23.25

Exponentiation

3^6
[1] 729

Modulus (remainder with division)

94%%5
[1] 4

Integer Division

54%/%7
[1] 7

Slightly more complex arithmetic operations

5-1+(4*3)/16*3
[1] 6.25