Base R
This is a very first introduction and overview to understand the fundamental idea, structure and basic syntax of R to get you started. We will walk you through the slides and demonstrate most examples, but you can also revisit the slides or check out the videos in your own time. For each chapter, you will find a piece called “code chunk”, which you can copy into your R environment and run so you get the same output as you see on the slides. This saves you a lot of copying and typing along, so make use of it.
There are small exercises with answer slides so you can get a hands-on experience. Try to do the exercises as they come and of course, you can consult the answer slides or videos if you get stuck.
Some parts of the provided material go a bit further than what we want to cover here and exceed the goal of our basic introduction. We will skip these for now and indicate at which slide you can stop, but if you are motivated and curious you can just keep going.
If you’re intrigued by the world of R and want to learn more, we offer an “Introduction to R” and many other courses - just check out our website. Have fun and don’t hesitate to ask for help!
Setup
Before we get started, we need two R Markdown files to work with. Follow the instructions below:
Create a folder called base-r on your computer.
Create an empty R Markdown file for coding along with the instructor’s examples. Open RStudio and click through File -> New File -> R Markdown -> give your file a title -> select HTML as output -> save the .Rmd file in the base-r folder.
Download the R Markdown file that you will use for the exercises baseR_exercises.Rmd and save it in the base-r folder.
Let’s get started: What is R and RStudio?
This describes the basic idea of R and RStudio and how to work in the RStudio environment.
Slides
For this part, just consider the slides 12 - 16.
Use this link to open slides in a new tab (refer to slide #12)
Code Chunk
This is an example of a so-called code chunk in which you write the actual code.
#let's define x
<- 6
x
x
<- "apple"
y y
You can insert chunks into your R Markdown file in the following ways:
- using a keyboard shortcut Ctrl + Alt + I on Windows or Cmd + Option + I on MacOS
- using the
Add Chunk
button in the editor toolbar - typing the chunk delimiters
{r} and
directly into the file
You can read more about code chunks on the R Markdown website.
Video
R Syntax & Data Types
Now we go a step further in the “language” of R and we learn how to assign variables and math functions, and what kind of different data types there are. We create vectors and lists. Just start at slide 18 and do the exercises as they come.
Slides
Use this link to open slides in a new tab (refer to slide #18)
Code Chunk
#let's define x
<- 1
x
x
*3
x
<- x + 2
y log2(y)
Video
Exercise 1
Do the following calculation in R: one plus five, divided by nine.
Assign the result of the calculation to a variable.
Test if the result of your calculation is larger than 1.
Round off the result to 1 decimal. Tip: use the Maths Functions section of the Base R cheat sheet!
Solution 1
Slides
Use this link to open slides in a new tab (refer to slide #27)
Video
Vectors
Here, we create vectors and learn what a vector actually is! Just start at slide 28 and do the exercises as they come.
Slides
Use this link to open slides in a new tab (refer to slide #28)
Code Chunk
c(1,2,3)
c("a", "b", "c")
c(T, TRUE, F)
c(TRUE, "a", 3)
# in mathematical operations
<- 1:5
p
pmean(p)
*2
p
<- 5:1
q
q
*q p
Video
Exercise 2
Meet Ann, Bob, Chloe, and Dan.
Make a character vector with their names, using the function c(). Save the vector as “name”.
How old are Ann, Bob, Chloe, and Dan? You decide! Design a numeric vector with their respective ages. Save it as “age”.
What is their average age? Use a function in R to calculate this. Tip: use the Maths Functions section of the Base R cheat sheet!
Solution 2
Slides
Use this link to open slides in a new tab (refer to slide #38)
Video
Data Structures
Here, we look at different data structures. Can we combine vectors, and lists? And how can we come up with a data frame? This will all be answered here. Just start at slide 41 and do the exercises as they come.
Slides
Use this link to open slides in a new tab (refer to slide #41)
Code Chunk
# vectors
# two vectors: name and age from exercise before
name
age
#combine vectors to a unidimensional vector:
c(name,age)
#combine vectors to multidimensional list:
list(name,age)
#combine vectors to twodimensional data frame:
data.frame(name,age)
#factors
<- c("UK", "USA", "USA", "UK")
country factor(country)
#usually as column in a data frame: a categorial variable
<- data.frame(name, age, country = factor(country))
df
df
summary(df)
Video
Exercise 3
Create a vector country containing four countries (use at least one duplicate!).
Create a data frame combining name, age, and country, and save it as df.
Check your dataframe with the function summary(). Does it contain a factor?
Make sure your column country is a factor, and confirm this with summary().
Create a list with your vectors name and age, and save it as mylist.
Solution 3
Slides
Use this link to open slides in a new tab (refer to slide #48)
Video
Missing data
What if for a data subject you do not have the data? How can you handle that in a data frame? We check here. Just start at slide 53 and do the exercises as they come.
Slides
Use this link to open slides in a new tab (refer to slide #53)
Code Chunk
# Data is not available: NA
$pet <- factor(c("cat", "none", "fish", NA))
df$pet df
Video
Exercise 4
Predict the results before you run the code. Does the real answer make sense to you?
Solution 4
Slides
Use this link to open slides in a new tab (refer to slide #57)
Indexing vectors and lists
Here we explain how you get to one specific value or column or data entry in your vector, or list. Just start at slide 66 and do the exercises as they come.
Slides
Use this link to open slides in a new tab (refer to slide #66)
Code Chunk
# in vector name:selecting by position
name2]
name[1:3]
name[c(2,2,1)]
name[
# in vector age: selcting by value
age> 40]
age[age > 40
age
name%in% c("Chloe", "Ann", "Evie")]
name[name
# selecting from list
mylist# selecting a list element
1]
mylist[# subselecting in the list element
1][2]
mylist[# selecting the content of a list element
1]]
mylist[[# subselecting in the content of a list element
1]][[2]] mylist[[
Video
Start the video at
Exercise 5
Return only the first number in your vector age.
Return the 2nd and 4th name in your vector name.
Return only ages under 30 from your vector age.
Return the name “Chloe” from your list mylist (see exercise 3).
Solution 5
Slides
Use this link to open slides in a new tab (refer to slide #76)
Video
Indexing a data frame
Here we explain how you get to one specific value or column or data entry in your data frame. Just start at slide 80 and do the exercises as they come.
Slides
Use this link to open slides in a new tab (refer to slide #80)
Code Chunk
#indexing columns
# by position
2]
df[,
# by name as a character string
"age"]
df[,
# by name, as an object
$age
df
#indexing rows
# by position
2,]
df[
# by content
$name ="Bob",]
df[df
#combining rows and columns
$name = "Bob", "age"] df[df
Video
Start the video at
Exercise 6
Before you start, please run this code:
rm(name,age,country)
From your dataframe df, return the entries for everyone living in a country of your choice.
Return only the names of everyone in your data frame df under 40. (Hint: what information should you use for row indexing? What information should you use for column indexing?)
Return the columns name and age together.
Solution 6
Slides
Use this link to open slides in a new tab (refer to slide #89)
Video
Recap
Slides
Use this link to open slides in a new tab (refer to slide #92)
You made it! This was the basic introduction to R to get you started. If you want to continue with the slides, feel free to do so :)