In this Session, you will take your first steps with R. 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!
You can execute R commands straight in the console. For example
## [1] 17
## [1] 18
In its most basic form, R can be used as a simple calculator. Consider the following arithmetic operators:
+-*/^%%^ operator raises the number to its left to the
power of the number to its right: for example 3^2 is
9.5 %% 3 is 2.With this knowledge, follow the instructions below to complete the exercise.
2^5 in the editor to calculate 2 to
the power 5.28 %% 6 to calculate 28 modulo
6.# symbol is used to add
comments on the R code.## [1] 10
## [1] 0
## [1] 15
## [1] 5
## [1] 32
## [1] 4
A basic concept in (statistical) programming is called a variable.
A variable allows you to store a value (e.g. 4) or an object (e.g. a function description) in R. You can then later use this variable’s name to easily access the value or the object that is stored within this variable.
You can assign a value 4 to a variable my_var with the
command: my_var <- 4
Over to you: complete the code in the editor such that it assigns
the value 42 to the variable x in the editor.
Click ‘Submit Answer’. Notice that when you ask R to print
x, the value 42 appears.
## [1] 42
(Perform experiments)
In this Session, you will learn how to work with vectors in R! After completing this lecture, you will be able to create vectors in R,name them, select elements from them and compare different vectors.
Vectors are one-dimension arrays that can hold numeric data, character data, or logical data. In other words, a vector is a simple tool to store data. For example, you can store your daily gains and losses in the stock market.
In R, you create a vector with the combine function
c(). You place the vector elements separated by a
comma between the parentheses. For example:
numeric_vector <- c(1, 2, 3)
character_vector <- c("a", "b", "c")
Once you have created these vectors in R, you can use them to do calculations.
Complete the code such that boolean_vector
contains the three elements: TRUE,
FALSE and TRUE (in that
order).
As a data analyst, it is important to have a clear view on the data that you are using. Understanding what each element refers to is therefore essential.
You can give a name to the elements of a vector with the
names() function. Have a look at this example:
some_vector <- c("John Doe", "poker player")
names(some_vector) <- c("Name", "Profession")
This code first creates a vector some_vector and then
gives the two elements a name. The first element is assigned the name
Name, while the second element is labeled
Profession. Printing the contents to the console yields
following output:
Name Profession
"John Doe" "poker player"
The code on the right names the elements in
poker_vector with the days of the week. Add code to do
the same thing for roulette_vector.
# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)
# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)
# Assign days as names of poker_vector
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
# Assign days as names of roulette_vectors
names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")It is important to know that if you sum two vectors in R, it takes the element-wise sum. For example, the following three statements are completely equivalent:
c(1, 2, 3) + c(4, 5, 6)
c(1 + 4, 2 + 5, 3 + 6)
c(5, 7, 9)
You can also do the calculations with variables that represent vectors:
a <- c(1, 2, 3)
b <- c(4, 5, 6)
c <- a + b
A_vector
and B_vector and it assign to
total_vector.total_vector.## [1] 5 7 9
In this section, our goal is to select specific elements of the
vector. To select elements of a vector (and later matrices, data frames,
…), you can use square brackets. Between the square brackets, you
indicate what elements to select. For example, to select the first
element of the vector, you type poker_vector[1]. To select
the second element of the vector, you type poker_vector[2],
etc. Notice that the first element in a vector has index 1, not 0 as in
many other programming languages.
By making use of comparison operators, we can approach the previous question in a more proactive way.
The (logical) comparison operators known to R are:
< for less than> for greater than<= for less than or equal to>= for greater than or equal to== for equal to each other!= not equal to each other6 > 5
returns TRUE. The nice thing about R is that you can use
these comparison operators also on vectors. For example:c(4, 5, 6) > 5
[1] FALSE FALSE TRUE
This command tests for every element of the vector if the condition
stated by the comparison operator is TRUE or
FALSE.
Check which elements in poker_vector are
positive (i.e. > 0) and assign this to
selection_vector. Print out
selection_vector so you can inspect it. The printout
tells you whether you won (TRUE) or lost
(FALSE) any money for each day.
## Monday Tuesday Wednesday Thursday Friday
## TRUE FALSE TRUE FALSE TRUE
In this session you will learn how to work with matrices in R. By the end of the chapter, you will be able to create matrices and to understand how you can do basic computations with them.
In R, a matrix is a collection of elements of the same data type (numeric, character, or logical) arranged into a fixed number of rows and columns. Since you are only working with rows and columns, a matrix is called two-dimensional.
You can construct a matrix in R with the matrix()
function. Consider the following example:
matrix(1:9, byrow = TRUE, nrow = 3)
matrix() function:1:9
which is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9).byrow indicates that the matrix is filled
by the rows. If we want the matrix to be filled by the columns, we just
place byrow = FALSE.nrow indicates that the matrix
should have three rows.Construct a matrix with 3 rows containing the numbers 1 up to 9, filled row-wise.
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
To help you remember what is stored in star_wars_matrix,
you would like to add the names of the movies for the rows. Not only
does this help you to read the data, but it is also useful to select
certain elements from the matrix.
rownames(my_matrix) <- row_names_vector
colnames(my_matrix) <- col_names_vector
We went ahead and prepared two vectors for you: region,
and titles. You will need these vectors to name the columns
and rows of star_wars_matrix, respectively.
colnames() to name the columns of
star_wars_matrix with the region
vector.rownames() to name the rows of
star_wars_matrix with the titles
vector.star_wars_matrix to see the
result of your work.## US non-US
## A New Hope 460.998 314.4
## The Empire Strikes Back 290.475 247.9
## Return of the Jedi 309.306 165.8