2+2
in the script window and hit Enter
.2+2
in the console window and hit Enter
.Ctrl + Enter
in the line where you are (in the script window), or a selected chunk or lines, to run the line in the console.+
-
*
/
^
%%
# 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
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
Numeric, integer, logic, character, factor, etc.
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
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
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:
x | y
x & y
!x
xor(x,y)
Not discussed today.
Vectors, matrices, data frames.
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"
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
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.