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:

  1. Create a folder called base-r on your computer.

  2. 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.

  3. 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 
x <- 6
x

y <- "apple"
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 
x <- 1
x

x*3

y <- x + 2
log2(y)

Video

Exercise 1

  1. Do the following calculation in R: one plus five, divided by nine.

  2. Assign the result of the calculation to a variable.

  3. Test if the result of your calculation is larger than 1.

  4. 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

p <- 1:5
p
mean(p)
p*2

q <- 5:1
q

p*q

Video

Exercise 2

Meet Ann, Bob, Chloe, and Dan.

  1. Make a character vector with their names, using the function c(). Save the vector as “name”.

  2. How old are Ann, Bob, Chloe, and Dan? You decide! Design a numeric vector with their respective ages. Save it as “age”.

  3. 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

country <- c("UK", "USA", "USA", "UK")
factor(country)

#usually as column in a data frame: a categorial variable

df <- data.frame(name, age, country = factor(country))
df

summary(df)

Video

Exercise 3

  1. Create a vector country containing four countries (use at least one duplicate!).

  2. Create a data frame combining name, age, and country, and save it as df.

  3. Check your dataframe with the function summary(). Does it contain a factor?

  4. Make sure your column country is a factor, and confirm this with summary().

  5. 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
df$pet <- factor(c("cat", "none", "fish", NA))
df$pet

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
name
name[2]
name[1:3]
name[c(2,2,1)]

# in vector age: selcting by value
age
age[age > 40]
age > 40

name
name[name %in% c("Chloe", "Ann", "Evie")]

# selecting from list
mylist
# selecting a list element
mylist[1]
# subselecting in the list element
mylist[1][2]
# selecting the content of a list element
mylist[[1]]
# subselecting in the content of a list element
mylist[[1]][[2]]

Video

Start the video at

Exercise 5

  1. Return only the first number in your vector age.

  2. Return the 2nd and 4th name in your vector name.

  3. Return only ages under 30 from your vector age.

  4. 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
df[,2]

# by name as a character string
df[, "age"]

# by name, as an object
df$age

#indexing rows
# by position
df[2,]

# by content
df[df$name ="Bob",]

#combining rows and columns
df[df$name = "Bob", "age"]

Video

Start the video at

Exercise 6

Before you start, please run this code:

rm(name,age,country)
  1. From your dataframe df, return the entries for everyone living in a country of your choice.

  2. 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?)

  3. 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 :)