# Beginner R lesson: arithmetic operations (part 1)

**R programming tutorials and exercises for data science and mathematics**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Topics covered:** number operations, defining and using variables

## Getting started

We start our programming journey by exploring how R lets us perform numerical calculations. We will first review some practical aspects relating to syntax and then dive into multiple example programs that provide a deeper insight into the language and R’s powerful number-crunching abilities.

Working with numbers is an essential part of many programs that we will create and showcases some basic syntax and functionality built into the R language. This lesson does not require any prior knowledge of R and is designed as a first contact with the language, so let’s dive in!

At a fundamental level R can serve as a powerful interactive calculator and the syntax we use for most arithmetic operations is fairly intuitive:

Addition and subtraction:

99 + 1 ## [1] 100 101 - 1 ## [1] 100

Multiplication and division:

100 * 2 ## [1] 200 100 / 3 ## [1] 33.33333

Powers:

10^2 ## [1] 100

Parentheses to define the order of operations:

(99 + 1) / 2 ## [1] 50

We can also define variables that store numerical or other types of data via an assignment operator:

a <- 100 a ## [1] 100 b <- 100 / 2 b ## [1] 50

Arithmetic operations can now be run using the variables we defined:

a + b ## [1] 150 a^b ## [1] 1e+100

We can also test whether two numbers are equal, which will be helpful in future lessons:

a ## [1] 100 b ## [1] 50 a == b ## [1] FALSE a == b * 2 ## [1] TRUE

Throughout this lesson we will also include comments in our code to clarify intended functionality and make the programs more easily readable. Everything in a line after the `#`

symbol will be treated by R as a comment and will not be executed:

# This is a comment 10^2 + 10^1 + 10^0 # The code before this comment will execute ## [1] 111

The concepts presented so far will serve as basic building blocks for more complex programs. Let’s see these topics in action by working through some examples.

## Practice: computing sums of consecutive integers

Many mathematical formulas have been constructed around sums of integer numbers. One such formula computes the sum of all integers between 1 and any integer `n`

greater than or equal to 1:

1 + … + n = n(n+1)/2 for any integer n >= 1.

Let’s use R to implement and test this formula for a few values of `n`

:

n <- 10 # Compute sum via formula: n * (n + 1) / 2 ## [1] 55 # Compute sum by explicit addition: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 ## [1] 55

As expected, computing this sum by explicitly adding each number together gives us the same answer as using the formula. We can also formally test if the outputs are equal without even looking at the actual results:

n * (n + 1) / 2 == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 ## [1] TRUE

Now that we have implemented this formula in R we can easily compute sums for new values of `n`

:

n <- 100 n * (n + 1) / 2 ## [1] 5050 n <- 1000 n * (n + 1) / 2 ## [1] 500500

Re-using the formula for different values of n is much easier and less error-prone than writing out the whole sum term by term, especially if n becomes large, but formulas are not always available to simplify repetitive computations. We will see in future lessons that R offers powerful ways to enable such tasks through loops and vector operations.

**leave a comment**for the author, please follow the link and comment on their blog:

**R programming tutorials and exercises for data science and mathematics**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.