Part 1
Introduction to R and RStudio
2nd - 4th July, 2024
R
and why should you use R
? --- (click here)R
& RStudio
--- (click here)RStudio
interface --- (click here)R
(click here)R
-projects --- (click here)R
d useR
s of R
About Sven?
About Tine?
Your position?
Research?
Expectations?
Various types of files:
Rmd-files (for slides) (.Rmd)
Qmd-files (for exercises) (.Qmd)
R-scripts (as example) (.R)
data sets (.sav, .csv, ...)
html-version of slides and exercises (.html)
All these files can be found at the dedicated web-page: https://icoworkshop-rstudio-2024.netlify.app/#quick-overview
R
k
We will provide you with the necessary information to start coding yourself.
🙏 Make it interactive! Feel free to ask questions!
Of course, we will also put you to woR
k! Do you get stuck? Is R
not working along?
🙏 Do not hesitate to ask our help!
R
and why should you use R
?R
?Powerful tool for:
A 'language'
→ We will have to learn some vocabulary and grammar ...
R
?Free AND open source!
In constant development
Thousands of "add-ins" (packages)
Almost every algorithm and technique is implemented in R
R
is ahead of commercial statistical software
Can facilitate reproducibility and open science practices
Large community (Help will always be given ...)
R
and RStudio
R
In case you haven't already installed R
. Do it now! Are you stuck? Let us know.
R
...RStudio
Click on Download RStudio
RStudio
Scroll down
Click on Download RStudio
Choose the right operating and download RStudio
In case you haven't already installed RStudio
. Do it now! Are you stuck? Let us know.
RStudio
interfaceRStudio
environment Let us discover RStudio
together!
R
vectors
matrices
arrays
data frames
lists
functions
...
R
; let's make a script ...Time to get started with the real work and code together
You can find the code on the next slides.
There is also a script Fruit.R
that contains the same code (see course material for Part 1 online)
We create a vector and call it Fruit
Fruit <- c("Apples", "Bananas", "Lemons", "Berries", "Peaches", NA)
c()
represents concatenate, all elements between the brackets ( separated by ,
) are 'merged' into one element
<-
means that we want to store the result in an object (a vector) that we call Fruit
the " "
that surround the elements indicate that these are of the type character
Let's look at the object Fruit by printing it to the console
Fruit
## [1] "Apples" "Bananas" "Lemons" "Berries" "Peaches" NA
str()
** function
str(Fruit)
## chr [1:6] "Apples" "Bananas" "Lemons" "Berries" "Peaches" NA
We create a vector called Weight
Weight <- c(230, 191, 93, 100, 48, 244)
str(Weight)
## num [1:6] 230 191 93 100 48 244
mean( )
mean(Weight)
## [1] 151
We create a vector called Yellow
Yellow <- c(F, T, T, F, F, F)
The element of the vector Yellow are 'logical operators' (TRUE or FALSE)
str(Yellow)
## logi [1:6] FALSE TRUE TRUE FALSE FALSE FALSE
Let us have a closer look at the vector Yellow
Yellow
## [1] FALSE TRUE TRUE FALSE FALSE FALSE
We create a data.frame called Fruit_data using the function data.frame( )
Fruit_data <- data.frame(Fruit, Weight, Yellow)
Fruit_data
## Fruit Weight Yellow## 1 Apples 230 FALSE## 2 Bananas 191 TRUE## 3 Lemons 93 TRUE## 4 Berries 100 FALSE## 5 Peaches 48 FALSE## 6 <NA> 244 FALSE
We can check the structure of the object Fruit_data
str(Fruit_data)
## 'data.frame': 6 obs. of 3 variables:## $ Fruit : chr "Apples" "Bananas" "Lemons" "Berries" ...## $ Weight: num 230 191 93 100 48 244## $ Yellow: logi FALSE TRUE TRUE FALSE FALSE FALSE
The $
operator is used to refer to a vector
Fruit_data$Weight
## [1] 230 191 93 100 48 244
[row, column]
Examples of indexing:
Retrieve element that is located at row 1 and column 1
Fruit_data[1,1]
## [1] "Apples"
Retrieve all elements in column 3
Fruit_data[,3]
## [1] FALSE TRUE TRUE FALSE FALSE FALSE
Retrieve all elements in row 3
Fruit_data[3,]
## Fruit Weight Yellow## 3 Lemons 93 TRUE
The univeR
sum is in constant development
Package = extensions of the Base
-functions
Range from specialised to generic/universal packages
Overview on https://cran.r-project.org/web/packages/available_packages_by_name.html
Which package(s) to use?
install.packages( )
and library( )
Packages can be downloaded and installed
by 'clicking' in RStudio
by using a function in the console or script
What Sven usually does: install.packages()
What Tine usually does: click and go!
For example,
install.packages("tidyverse", dependencies = T)
Packages should be activated at the start of the session
by 'clicking' in RStudio
by using a function in de console or script
What we both usually do: library()
For example,
library("tidyverse")
R
-projectsDuring analyses, we will regularly handle different files:
Consequently, we should refer to these files in our code.
There are two ways to refer to these files.
USING ABSOLUTE PATHS
We can do this using an ABSOLUTE path. Below is an example of an absolute path:
'c:/Users/Sven/Mijn Documenten/UAntwerpen/Analyses/ProjectX/R_Script/Analysescript1.R'
Absolute paths make it difficult to share your work; you get into trouble when you change laptops; ...
USING RELATIVE PATHS
To avoid these problems, RStudio
introduced the concept of an R
-Project.
Within a project you can use RELATIVE paths (that starts from the folder in which you save the project).
Below is an example of a relative path:
'~R_Script/Analysescript1.R'
If I put a project of Sven on my laptop, the RELATIVE paths might get me into trouble.
For example, the Rmd-file 'Slides_part1.Rmd' is located at
'c:/Users/Sven/Dropbox/ICO_R_2022/Presentations/Part 1/Slides_part1.Rmd'
Of course, I do not have the same structure on my laptop...
To facilitate sharing of projects, the package here
has been created.
here()
The function here()
creates a paths relative to the top-level directory of my laptop.
library(here)here()
## [1] "/Users/demaeyer/Documents/Praatjes/Workshops/R Course ICO 2024"
here("Presentations", "Part 1", "Slides_part1_Rmd")
## [1] "/Users/demaeyer/Documents/Praatjes/Workshops/R Course ICO 2024/Presentations/Part 1/Slides_part1_Rmd"
Creating a project in RStudio
You can find the folder Exercises
at the Github: https://github.com/Sdemaeyer2/R_workshop_ICO2024/. Download this folder and put it somewhere on your laptop.
RStudio
Click on File/New Project...
Next, you can choose:
Data exist in various formats, but the most common ones are:
Various methods and packages have been developed to import these types of data
We will show some possibilities
readxl
pakketMake sure that the package is installed on your laptop Then, you can use the code below to import the data
library(readxl)Data <- read_excel( path = "<path to map and file>")
Data_Groep2 <- read_excel( path = "<path to map and file>/Datacollection1.xlsx", sheet = "Group1")
sheet
if:
foreign
Make sure that the package is installed on your laptop Then, you can use the code below to import the data
library(foreign)Data <- read.spss( file = "<path to map and file>", use.value.labels = FALSE, to.data.frame = TRUE)
use.value.labels
argument:
In SPSS, data is labelled. Do you want these labels to appear in the R data-frame?
For example. Variable with 3 categories: 1 = "Low", 2 = "Average", 3 = "High". These labels are included in the SPSS-file. You can choose to import these labels in the R data frame (use.value.labels = TRUE
) or only the numbers 1, 2 and 3 that represent these categories (use.value.labels = FALSE
)
read.table( )
csv-files usually look like this (separated by ,
or ;
)
Column1, Column2, Column31, 3, 52, 4, 68, 10, 99
To import these data, the function read_table( )
is most convenient...
Data <- read.table( file = "<path to map and file>", header = TRUE, sep = ",", dec = ".")
header
argument:
The first row includes column (variable) names or not?
sep
argument:
Which character is used to separate the values in the different columns?
dec
argument:
Which character is used to indicate a decimal point? (Can be a point or a comma)
R
and why should you use R
? --- (click here)R
& RStudio
--- (click here)RStudio
interface --- (click here)R
(click here)R
-projects --- (click here)Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |