Basic Operations

Let’s take a quick look at the most important basic operations in R. You can also use a cheat sheet to keep an overview during the course.

Basic Mathematical Operations

We can use R as a calculator:

(1 + 2) * 3^2
2 - 3/log(8)

Assignment Operator

We can create objects in R by using the assignment operator <-, which assigns a value to an object:

## Assign the result of 1 + 1 to the object 'result':
result <- 1 + 1
result
[1] 2
## Assign the result of the comparison to the object 'log_result':
log_result <- 10 > 1
log_result
[1] TRUE

Combination of Elements

We can combine multiple elements to build a new one:

new_element <- c(1, 10, 15)

In this case, this new element is a vector, which is a one dimensional collection of values. The c() stands for combine, or concatenate, and is the basic function for building a vector out of single elements.

Comparisons and Logical Operators

The boolean variables in R are TRUE and FALSE. Comparison operators return either TRUE or FALSE:

1 < 2
[1] TRUE
# But:
2 < 1
[1] FALSE

These are the comparison operators you will typically use:

Operator Description
< less than
> greater than
== equal to
!= not equal to
<= less or equal
>= greater or equal
%in% part of

Mainly we will use these logical operations to check which elements in a vector satisfy some requirements:

# Build a vector of numbers ranging from 1 to 10
vec_num <- 1:10

# Check which of these numbers are smaller than 5
vec_num < 5
 [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE

This will become important later on, when we want to subset vectors and data frames to extract only those values that satisfy some requirements we’ve defined.

%in%

The %in% operator is used to check for each element of its first argument if it is part of the second argument:

c("Monica", "Rachel", "Barny") %in% c("Monica", "Rachel", "Ross", "Joey", "Phoebe", "Chandler")
[1]  TRUE  TRUE FALSE

!

We can invert boolean values by using !:

!TRUE
[1] FALSE
!(1 > 100)
[1] TRUE

| (“or”) and & (“and”)

We can also combine multiple logical operations by using | (“or”) and/or & (“and”):

TRUE & FALSE
[1] FALSE
TRUE | FALSE
[1] TRUE
(10 < 20 | "a" == "b")
[1] TRUE
(10 < 20 & "a" == "b")
[1] FALSE
!(10 < 20 & "a" == "b")
[1] TRUE

Functions

Everything that does something in R is a function. A function call has the form: functionname(argument1 = value, argument2 = value, ...). One basic example is the function that can calculate the square root:

sqrt(4)
[1] 2

We can also assign the name of the function argument to our value. This is clearer, as we don’t rely on the order of the function arguments:

rep(4, 10)
 [1] 4 4 4 4 4 4 4 4 4 4

will rep 4 10 times. If we swap the arguments, the 10 will be repeated 4 times:

rep(10, 4)
[1] 10 10 10 10

But if we specify which value belongs to which function argument, the order doesn’t matter:

rep(times = 10, x = 4)
 [1] 4 4 4 4 4 4 4 4 4 4

How do we know which arguments a function has? By using the documentation:

HELP!

One of the most important functions in R is the help-function ?:

?rep

will open the documentation for the function with the description of its usage, details about the arguments … Take a look and become acquainted with the structure of the function documentation, it is an important tool!