4 + 2Week 5 - Presentation of R and its basic functions
CHU0387 | National Taiwan Normal University
Overview
This week, we will have a tutorial on how to prepare your computer for further analyses.
Get to know R and RStudio
- Basics: What does RStudio look like?
- Hands-on: What are the most important functions in R? Write your very first lines of R code!
Please complete the following tasks before our next meeting:
Please complete the following tasks before our next meeting:
- Download the script of this week
- Annotate each line of code (use your own words, these annotations are for your own reference!)
- Upload the annotated script on Moodle.
💻 1. The Interface of RStudio
Now I assume that R and RStudio are correctly installed. When you start RStudio, you will see a lot of things on your screen???many blocks, many menus. It can be quite overwhelming. No worries! This is exactly what this section is about.
General Overview: Blocks, Blocks, Blocks!
When you open RStudio for the very first time, you should see something similar to the picture below. (Note: If yours looks slightly different, that is completely normal!)

The best way to apprehend RStudio is to see the interface as a collection of blocks, each serving a specific purpose. There are four main blocks. I give them unofficial names to make them easier to remember:
- 💻 The Computer Block
- 🗂️ The Variable Block
- 📝 The Script Block
- ⚙️ The Console Block

(You may not be able to see the ‘Script block’ on your computer just yet. Don’t worry, we will open it soon!)
Click on the Files button in this block. You will see a list of files and folders from your computer. This interface allows you to communicate and navigate directly with your hard drive, right from inside RStudio!
Take a second to play with it: Click on the folders. Try to navigate to your Documents or Desktop. There is no mystery here???it works exactly like navigating folders on your computer normally does!
The most important tab here is the Environment. This is where variables you create while coding are temporarily stored. Let’s demonstrate this. In the ‘Console block’ (bottom-left), type this code and press Enter:
And indeed, the output shows 6. Now, let’s play a game. You want to obtain the number 8, but you have to use 4 + 2. You could write:
4 + 2 + 2But what if the math gets incredibly complex? This is where variables come in. Try this code:
a <- 4 + 2Nothing appears in the console anymore. But look at your Variable block!

You have stored the calculation inside a variable called a. You can call it anytime simply by typing a in the console, or even add to it by typing a + 2!
You just had a glimpse of it. The console block is the engine room where the code is actually run. When you type commands here and press Enter, R executes them immediately.
(There are also Terminal and Background Jobs tabs here, but we can safely ignore them for this tutorial).
The Script block is your digital notepad. If you type code in the Console, it disappears when you close RStudio. If you want to save hundreds of lines of code, you need a Script!
Let’s open one. Click “New File”, and then select “R Script”.

Type the following lines into your new Script block:
4 + 2
a <- 4 + 2
a
a + 2Select all the text with your mouse, and click ‘Run’.

Now you can go to File > Save and save this script to your computer. The next time you open it, all your code will still be there!
🎨 Exercise: Make it your own!
Your four blocks are like construction toys. You can change their size by dragging the borders, or even change the background color to a dark theme!
Path: Tools > Global Options... > Appearance > Editor theme

2. Libraries (Packages)
You can do amazing things with R: draw maps, run complex statistics, and manipulate massive datasets. But it can feel overwhelming to write all that code from scratch.
Good news! R is a collaborative, open-source community. Nobody writes all their code from scratch! We rely on “Packages” or “Libraries” built by other people.
| Concept | Definition |
|---|---|
| Line of code | A single, simple instruction (e.g., 4 + 2). |
| Function | A pre-packaged cluster of code that performs a specific task. |
| Package / Library | A downloadable folder containing hundreds of useful functions! |
There are thousands of packages available for free, often coming with their own fun hex-logos.

How to install and use Libraries
You only need to install a package once, but you must load it every time you open RStudio.
In your Console, type this to install ggplot2 (a famous plotting package):
install.packages("ggplot2")Once it finishes installing, you load it into your current session by typing:
library(ggplot2)Imagine you want to cook a complex dinner.
install.packages()is like going to the bookstore to buy a recipe book. You bring it home and put it on your shelf.library()is like taking the book off the shelf and opening it on your kitchen counter. You have to open it every time you want to use those specific recipes!
3. Essential R Syntax & Data
Before doing anything with data, you must tell R exactly which folder on your computer to look in. This is your Working Directory.
Use the Computer Block to navigate to your project folder. Click the Gear Icon, and select “Save as Working Directory”.

Use the # symbol to write notes to yourself. R will completely ignore anything written after a hashtag!
### This is a comment. R will not run this.
2 + 4 ### This adds two numbers together!Let’s import a .csv dataset (like an Excel file). We use the read.csv() function and assign it to a variable called data.
data <- read.csv("GACTT_RESULTS_ANONYMIZED_v2.csv", header=TRUE)Alternatively, you can use the RStudio interface! In your Variable block, click “Import Dataset” > “From Text (base)”, select your file, and click Import.

Once loaded, you can inspect your data using str(data) to see its structure, or summary(data) to get a quick mathematical overview.
Often, survey data comes in as plain text when it should be categorized into groups (Factors). We fix this using as.factor():
data$What.is.your.age. <- as.factor(data$What.is.your.age.)(The $ symbol tells R to look inside the dataset for a specific column!)
If we want to count how many people fit into different groups, we can use the powerful dplyr package:
library(dplyr)
data3 <- data2 %>%
group_by(What.is.your.age., How.many.cups.of.coffee.do.you.typically.drink.per.day.) %>%
summarize(Count = n())
With our data summarized, we can use the built-in plot() function to visualize it:
plot(x = data3$What.is.your.age., y = data3$Count)(If your groups are out of alphabetical order, you can use the factor(..., levels=c(...)) command to manually sort them before plotting!)
Please complete the following tasks before our next meeting:
- Download the script of this week
- Annotate each line of code (use your own words, these annotations are for your own reference!)
- Upload the annotated script on Moodle