1. Download the Intro_Examples.R file to a specified Folder (“e.g. Statistics”).
  2. Open RStudio.
    • Identify the script window, the console window and the environment window.
    • Write 2+2 in the script window and hit Enter.
    • Write 2+2 in the console window and hit Enter.
  3. Change your working directory to “Statistics”" (or where you downloaded the Example.R file).
    • Session \(\rightarrow\) Set Working directory \(\rightarrow\) Choose directory.
  4. Open the file Example.R.

Basic calculations

# An addition
2 + 5 
## [1] 7
# A subtraction
10 - 5 
## [1] 5
# A multiplication
2 * 6
## [1] 12
# A division
(6 + 2) / 2 
## [1] 4
# Exponentiation
10^2
## [1] 100
# Modulo
11%%3
## [1] 2

Variable assignment

Long long time ago we used <-. Today it can be done with <- or =.

Assign the value 50 to x

x <- 50

Print out the value of the variable x

x
## [1] 50

Assign the value 2 to y and sum x + y

y <- 2
x + y
## [1] 52

Data type/classes

Numeric, integer, logic, character, factor, etc.

Numeric class

Stores any numeric data.

class(x)
## [1] "numeric"
is.numeric(x)
## [1] TRUE
is.integer(x)
## [1] FALSE
x_int <- as.integer(x)
is.integer(x_int)
## [1] TRUE

String class

Stores all kinds of text data.

my_name <- "Juan"
my_name
## [1] "Juan"
class(my_name)
## [1] "character"
is.character(my_name)
## [1] TRUE
is.character(x)
## [1] FALSE
is.numeric(my_name)
## [1] FALSE
# x + my_name

Logical class

Stores boolean truth values, i.e. TRUE and FALSE. Numeric values can be converted to logical (0 = false, all others = true).

3 > 2
## [1] TRUE
2 > 3
## [1] FALSE
3 == 3
## [1] TRUE
4 == 3
## [1] FALSE
4 >= 3
## [1] TRUE
my_True <- 3 > 2
class(my_True)
## [1] "logical"
is.logical(my_True)
## [1] TRUE
as.logical(0)
## [1] FALSE
as.logical(1)
## [1] TRUE

Example of logical operations:

  • Or x | y
  • And x & y
  • Not !x
  • Xor xor(x,y)

Factors

Not discussed today.

Basic data structures

Vectors, matrices, data frames.

Vectors

One-Dimensional arrays. All elements must have the same type. They are declared with c(...).

v <- c("Juan", 2, TRUE)
v
## [1] "Juan" "2"    "TRUE"
class(v)
## [1] "character"
w <- c(1, 2, 3)
w
## [1] 1 2 3
class(w)
## [1] "numeric"

Matrix

Two dimensional version of a vector. Also requires all data to be of the same type.

Produce a matrix

A <- matrix( c(2, 4, 3, 1, 5, 7), # the data elements 
             nrow = 3,              # number of rows 
             ncol = 2,              # number of columns 
             byrow = FALSE)        # fill matrix by rows 
A
##      [,1] [,2]
## [1,]    2    1
## [2,]    4    5
## [3,]    3    7

Access elements of the matrix

A[1,] # first row
## [1] 2 1
A[,1] # first column
## [1] 2 4 3
A[c(1,3),] # row 1 and 3
##      [,1] [,2]
## [1,]    2    1
## [2,]    3    7
A[2,2] #element in second row, second column
## [1] 5

Data frame

Used to store data with columns of different data-types.

students <- c("Juan","Johan","Joan","John","Giovanni")
country <- c("Mexico","Sweden","Sweden","England","Italy")
age <- c(20,21,24,19,30)
is_exchange <- c(TRUE,FALSE,FALSE,TRUE,TRUE)
my_class <- data.frame(Name = students, Country = country, Age = age, 
                       Exchange = is_exchange, stringsAsFactors = FALSE)
my_class

Access data from a data frame. It can be done in the same way as with matrices, e.g. with the [r,c] notation. It can also be done using the $ notation to access specific columns.

my_class$Age
## [1] 20 21 24 19 30
my_class$Country
## [1] "Mexico"  "Sweden"  "Sweden"  "England" "Italy"
my_class$Exchange
## [1]  TRUE FALSE FALSE  TRUE  TRUE

There are many ways to produce matrices and data frames, the previous ones are just few examples.