#### QCBS Workshop 1
#### R reference script
## Novembre 2020
# Introduction ------------------------------------------------------------
## How to read the console
output <- "This is the output"
output
# Using R as a calculator -------------------------------------------------
## Additions and Substractions
1 + 1
10 - 1
## Multiplications and Divisions
2 * 2
8 / 2
## Exponents
2^3
## Challenge 2
# Use R to calculate the following testing question.
2 + 16 * 24 - 56
## Challenge 3
# Use R to calculate the following testing question.
2 + 16 * 24 - 56 / (2 + 1) - 457
## Challenge 4
### What is the area of a circle, with a radius of 5cm?
3.1416 * 5^2
### Note that R has some built-in constants such as pi,therefore:
pi * 5^2
# Objects -----------------------------------------------------------------
# Objects are one of the most useful concepts in R.
# You can store values as named objects using the assigment operator "<-"
objectName <- "assignedValue"
## Objects naming: good practices
# Try to have short and explicit names
# Adding spaces before the "<-" is recommended
# When typing the object names, R will return its value
mean_x <- (2 + 6) / 2
mean_x
## Challenge 5
# Create an object with a value of 1+1.718282 (Euler's number) and name it "euler.value"
euler_value <- 1 + 1.718282
euler_value
## Challenge 6
# Create a second object with a name that starts with a number, What happens?
#8euler_value <- 8 * (1 + 1.718282)
# Types of data structures in R -------------------------------------------
# - Vectors
# - Data frames
# - Matrices, arrays and lists
## Vector
# - An entity consisting of a list of related values
# - A single value is called an *atomic value*
# - All values of a vector must have the **same mode** (or class).
# * Numeric: only numbers
# * Logical: True/False entries
# * Character: Text, or a mix of text and other modes
# Creating vectors require the c() function // c() stands for combine or concatenate
vector <- c("value1", "value2")
## Numeric vectors
num_vector <- c(1, 4, 3, 98, 32, -76, -4)
num_vector
## Character vectors
char_vector <- c("blue", "red", "green")
char_vector
## Logical vectors
bool_vector <- c(TRUE, TRUE, FALSE)
bool_vector
bool_vector2 <- c(T, T, F)
bool_vector2
## Challenge 7
# Create a vector containing the first 5 odd numbers, starting from 1, and name it "odd_n"
odd_n <- c(1, 3, 5, 7)
## Object structure with the dput() function
dput(odd_n)
## We can use vectors for calculations
x <- c(1:5)
y <- 6
x + y
x * y
## Data frames
# - Used to store data tables
# - A list of vectors of the same length
# - Columns = variables
# - Rows = observations, sites, cases, replicates, ...
# - Differents columns can have different modes
## One way to create vectors
# Start by creating vectors
site_id <- c("A1.01", "A1.02", "B1.01", "B1.02")
soil_pH <- c(5.6, 7.3, 4.1, 6.0)
num_sp <- c(17, 23, 15, 7)
treatment <- c("Fert", "Fert", "No_fert", "No_fert")
# We then combine them using the data.frame() function
my_df <- data.frame(site_id, soil_pH, num_sp, treatment)
my_df
# Data frame structure with the dput() function
dput(my_df)
# Creation of a new data frame, identical to the previous one with the structure() function
structure(list(site_id = structure(1:4, .Label = c("A1.01", "A1.02", "B1.01", "B1.02"), class = "factor"),
soil_pH = c(5.6, 7.3, 4.1, 6),
num_sp = c(17, 23, 15, 7), treatment = structure(c(1L, 1L, 2L, 2L), .Label = c("Fert", "No_fert"), class = "factor")),
class = "data.frame", row.names = c(NA, -4L))
## Matrices, Arrays and Lists
## Indexing vectors
# You can use indexing to chose a particular position,
# let's say we want to see the second value of our `odd_n` vector
odd_n[2]
# It also work with multiple positions:
odd_n[c(2, 4)]
# It can be used to remove some values at particular positions
odd_n[-c(1, 2)]
# If you select a position that is not in the vector:
odd_n[c(1, 6)]
# You can also use conditions to select values:
odd_n[odd_n > 4]
# You can also use conditions to select values
char_vector[char_vector == "blue"]
## Challenge 8
# Using the vector "num_vector"
# - Extract the 4th value
# - Extract the 1st and 3rd values
# - Extract all values except for the 2nd and the 4th
num_vector[4]
num_vector[c(1, 3)]
num_vector[c(-2, -4)]
## Challenge 9
# Explore the difference between these 2 lines of code:
char_vector == "blue"
char_vector[char_vector == "blue"]
## Indexing data frames
## Challenge 10
# 1. Extract the `num.sp` column from `my_df` and multiply its value by the first four values of `num.vec`.
my_df$num_sp * num_vector[c(1:4)]
# or
my_df[, 3] * num.vector[c(1:4)]
# 2. After that, write a statement that checks if the values you obtained are greater than 25.
(my_df$num.sp * num.vector[c(1:4)]) > 25
# Function ----------------------------------------------------------------
# A function is a tool to simplify your life.
#
# It allows you to quickly execute operations on objects without having to write every mathematical step.
#
# A function needs entry values called **arguments** (or parameters). It then performs hidden operations using these arguments and gives a **return value**.
# To use (call) a function, the command must be structured properly, following the "grammar rules" of the `R` language: the syntax.
# function_name(argument 1, argument 2)
## Arguments
# Arguments are **values** and **instructions** the function needs to run.
# Objects storing these values and instructions can be used in functions:
a <- 3
b <- 5
sum(a, b)
## Challenge 11
# - Create a vector `a` that contains all the numbers from 1 to 5
# - Create an object `b` with a value of 2
# - Add `a` and `b` together using the basic `+` operator and save the result in an object called `result_add`
# - Add `a` and `b` together using the sum() function and save the result in an object called `result_sum`
# - Compare `result_add` and `result_sum`. Are they different?
# - Add 5 to `result_sum` function using the sum() function
a <- c(1:5)
b <- 2
result_add <- a + b
result_sum <- sum(a, b)
result_add
result_sum
sum(result_sum, 5)
## Arguments
# Arguments each have a **name** that can be provided during a function call.
# If the name is not present, the order of the arguments does matter.
# If the name is present, the order does not matter.
a <- 1:100
b <- a^2
plot(a, b)
plot(b, a)
plot(x = a, y = b)
plot(y = b, x = a)
# Package -----------------------------------------------------------------
#To install packages on your computer, use the install.packages() function.
# install.packages("packageName")
#Installing a package is not enough to use it. You need to load it into your workspace
# Use the library() function
install.packages("ggplot2")
qplot(1:10, 1:10)
library(ggplot2)
qplot(1:10, 1:10)
# Getting help ------------------------------------------------------------
# Searching for functions
#
# To find a function that does something specific in your installed packages, you can use `??` followed by a search term.
#
# Let's say we want to create a *sequence* of odd numers between 0 and 10 as we did earlier. We can search in our packages all the functions with the word "sequence" in them:
??sequence
# OK! SO let's use the seq() function!!
#
# But wait... how does it work? What arguments does it need?
#
# To find information about a function in particular, use `?`
#
?seq
## Challenge 13
# 1. Create a sequence of even numbers from 0 to 10 using the seq() function.
seq(from = 0, to = 10, by = 2)
seq(0, 10, 2)
# 2. Create a unsorted vector of your favourite numbers, then sort your vector in reverse order.
numbers <- c(2, 4, 22, 6, 26)
sort(numbers, decreasing = T)
## Challenge 14
#
# Find the appropriate functions to perform the following operations:
#
# - Square root
# - Calculate the mean of numbers
# - Combine two data frames by columns
# - List availables objects in your workspace
?sqrt
?mean
?cbind
?ls
# Additional resources ---------------------------------------------------
# 1. Cheatsheets:
# - www.rstudio.com/resources/cheatsheets/
# 2. Websites:
# - http://r4ds.had.co.nz/index.html
# - https://cran.r-project.org/doc/manuals/r-release/R-intro.html
# - http://cran.r-project.org/doc/contrib/Baggott-refcard-v2.pdf
# - http://statmethods.net/
# - https://support.rstudio.com/hc/en-us/categories/200035113-Documentation
# - http://cookbook-r.com/
## Thank you for attending!
########################### END OF SCRIPT ################################