Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. R is not only good for analysing and visualizing data, but also for solving maths problems or comparing data with each other. Plus you can use it just like a pocket calculator.

• Use R as a pocket calculator
• Use arithmetic operators on vectors
• Use relational operators on vectors
• Use logical operators on vectors
___ + ___
___ - ___
___ / ___
___ * ___
___ ^ ___

___ == ___
___ != ___
___ < ___
___ > ___
___ = ___

___ & ___
___ | ___

___ %in% ___

## Using R as a pocket calculator

___ + ___
___ - ___
___ / ___
___ * ___

R is a programming language mainly developed for statistics and data analysis. Within R you can use mathematical operators just like you would use on a calculator. For example, you can add + and subtract - numbers from each other:

5 + 5
 10
7 - 3.5
 3.5

Similarly, you can multiply * or divide / numbers:

5 * 7
 35
8 / 4
 2

You can take the power of a number by using the ^ sign:

2 ^ 3
 8

According to the rules of mathematics, you can use round brackets to specify the order of evaluation in more complex tasks:

5 * (2 + 4 / 2)
 20

## Exercise: Use basic arithmetic

To calculate the mean of the numbers 2, 3, 7 and 8:

1. Add all the numbers together using +.
2. Divide the result by the number of elements.
3. Make sure that the result of the addition is calculated first by using braces ().
Start Exercise

## Applying arithmetic operators on vectors

___ + ___
___ - ___
___ / ___
___ * ___

Operations, such as addition, subtraction, multiplication and division are called arithmetic operations. They can not only operate with single values but also with vectors. If you use arithmetic operations on vectors, the operation is done on each individual number from the first vector and the individual number at the same position from the second vector.

In the following example we create two numeric vectors and assign them to the variables a and b. We then add them together:

a <- c(1, 3, 6, 9, 12, 15)
b <- c(2, 4, 6, 8, 10, 12)
a + b
  3  7 12 17 22 27

As the output shows, the first elements of the two vectors were added together and resulted in 1 + 2 = 3. The second elements added up to 3 + 4 = 7, the third elements to 6 + 6 = 12 and so on.

We can apply any other arithmetic operation in a similar way:

a <- c(22, 10, 7, 3, 14, 4)
b <- c(4, 5, 2, 6, 14, 8)
a / b
 5.5 2.0 3.5 0.5 1.0 0.5

Using the same principle, the first element of the result is 22 / 4 = 5.5, the second is 10 / 5 = 2 and so on.

## Quiz: Vector Multiplication

odd <- c(1, 3, 5)
even <- c(2, 4, 6)
odd * even
Inspect the code chunk above. What is the result of the multiplication?
• 108
• 54
• 15, 48
• 2, 12, 30
• 18, 36, 54
Start Quiz

## Exercise: Multiply numeric vectors

Multiply the numeric vectors ascending and descending:

1. Create a vector with the numbers 1, 2, 3 and 4 and assign it to the variable ascending.
2. Create a vector with the numbers 4, 3, 2 and 1 and assign it to the variable descending.
3. Multiply (*) the variable ascending with the variable descending.
Start Exercise

## Using relational operators

___ == ___
___ != ___
___ < ___
___ > ___
___ = ___

Relational operators are used to compare two values. The output of these operations is always a logical value TRUE or FALSE. We distinguish six different types relational operators, as we’ll see below.

The equal == and not equal != operators check whether two values are the same (or not):

2 == 1 + 1
 TRUE
2 != 3
 TRUE

The less than < and greater than > operators check, whether a value is less or greater than another one:

2 > 4
 FALSE
2 < 4
 TRUE

The less than or equal to <= and the greater than or equal to >= operators combine the check for equality with either the less or the greater than comparison:

2 >= 2
 TRUE
2
 TRUE

All of these operators can be used on vectors with one or more elements as well. In that case, each element of one vector is compared with the element at the same position in the other vector, just as with the mathematical operators:

vector1 <- c(3, 5, 2, 7, 4, 2)
vector2 <- c(2, 6, 3, 3, 4, 1)
vector1 > vector2
  TRUE FALSE FALSE  TRUE FALSE  TRUE

Therefore, the output of this example is based on the comparisons 3 > 2, 5 > 6, 2 > 3 and so on.

## Exercise: Compare numeric values

Use the appropriate relational operator and check whether 3 is greater than or equal to 2

Start Exercise

## Exercise: Compare temperatures

In the following exercise, we make use of the weather data gathered by the city of Innsbruck over the last decades. You are given two variables, avgtemp_1997_2006 and avgtemp_2007_2016, each containing the monthly average temperatures in Innsbruck for the years 1997 to 2006 and 2007 to 2016.

Use an appropriate relational operator and check in which months there was an increase in the average temperature.

Start Exercise

## Using logical operators

___ & ___
___ | ___

The AND operator & is a used for checking whether multiple statements are TRUE at the same time. Using a simple example, we could check whether 3 is greater than 1 and at the same time if 4 is smaller than 2:

3 > 1 & 4 < 2
 FALSE

3 is in fact greater than 1, but 4 is not smaller than 2. Since one of the statements is FALSE, the output of this joined evaluation is also FALSE.

The OR operator | checks only, whether any of the statements is TRUE.

3 > 1 | 4 < 2
 TRUE

In an OR statement, not all elements have to be TRUE. Since 3 is greater than 1, the output of this evaluation is TRUE as well.

The ! operator is used for the negation of logical values, which means it turns TRUE values to FALSE and FALSE values to TRUE. If we have a statement resulting in a logical TRUE or FALSE value, we can negate the result by applying the ! operator on it. In the following example we check whether 3 is greater than 2 and then negate the result of this comparison:

!3 > 2
 FALSE

Logical operators, just like arithmetic and relational operators, can be used on longer vectors as well. In the following example we use three different vectors a, b and c and try to evaluate multiple relations in combination.

a <- c(1, 21, 3, 4)
b <- c(4, 2, 5, 3)
c <- c(3, 23, 5, 3)

a>b & b
 FALSE  TRUE FALSE FALSE

First, both relational comparisons a>b and b<c are evaluated and result in two logical vectors. Therefore, we essentially compare the following two vectors:

c(FALSE, TRUE, FALSE, TRUE) & c(FALSE, TRUE, FALSE, FALSE)
 FALSE  TRUE FALSE FALSE

The & operator checks whether both values at the same position in the vectors are TRUE. If any value of the pairs is FALSE, the combination is FALSE as well.

The | operator checks whether any of the values at the same position in the vectors is TRUE.

c(FALSE, TRUE, FALSE, TRUE) | c(FALSE, TRUE, FALSE, FALSE)
 FALSE  TRUE FALSE  TRUE

## Exercise: Use the & operator

You are given three variables alpha, beta and gamma. Use an appropriate logical operator and check whether alpha is greater than beta and at the same time gamma is smaller than beta.

Start Exercise

## Exercise: Use the | operator

You are given three variables alpha, beta and gamma. Each contains a numeric vector of two elements. Use the appropriate logical operator and check whether alpha is greater than beta OR gamma is less than beta. (Hint: use the logical OR operator |)

Start Exercise

## Using the %in% operator

___ %in% ___

One additional, often used special operator is the %in% operator. It checks whether or not the contents of one vector are present in another one as well.

In the following example we use the variable EU containing the abbreviation of all countries in the European Union. Then, we check whether or not the character "AU" is present in the EU variable.

EU <- c("AU","BE","BG","CY","CZ","DE","DK","EE","ES","FI","FR","GR","HR","HU",
"IE","IT","LT","LU","LV","MT","NL","PO","PT","RO","SE","SI","SK")
"AU" %in% EU
 TRUE

The following example extends the search and compares multiple elements with the contents of the EU variable. It outputs a logical vector as a result containing a logical value for each element:

c("AU","HU","UK") %in% EU
  TRUE  TRUE FALSE

As the output shows, the first two character elements "AU" and "HU" are present in the variable EU, however the third element "UK" is not.

## Exercise: Use the %in% operator

You are standing in the supermarket and need to determine which you can check-off your shopping_list:

• Use the %in% operator and determine which shopping_list items you can check-off your list based on the items in your basket.
• Print the output of the resulting vector to the console.
Start Exercise

Use basic operators is an excerpt from the course Introduction to R, which is available for free on quantargo.com

VIEW FULL COURSE