Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to R for Data Analysis ยท Lesson 3 of 9

Module Two

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
3

Module 2: Installing R and Getting Started with RStudio

Module Introduction

In Module 1, we learned that R is a programming language used for data analysis, statistics, and data visualisation.

Before you can start analysing data with R, you need to install the necessary software on your computer. You will also learn how to use RStudio, a popular application that makes working with R easier.

Think of R as the engine of a car and RStudio as the dashboard that helps you control and use the engine conveniently.

Learning Objectives

By the end of this module, you should be able to:

  • Understand the difference between R and RStudio.
  • Download and install R on a computer.
  • Download and install RStudio.
  • Understand the RStudio interface.
  • Identify the Console and Script Editor.
  • Write and run your first R command.
  • Save and open an R script.

1. Understanding R and RStudio

What Is R?

R is a programming language that allows you to perform calculations, analyse information, organise datasets, and create charts.

It is the main software responsible for carrying out the instructions you write when working with R.

What Is RStudio?

RStudio is an application that provides a convenient working environment for writing and running R code. It helps you manage your scripts, view datasets, install packages, and examine the results of your analysis.

RStudio is developed by Posit and is commonly used by data analysts, researchers, and programmers.

Important difference:

  • R is the programming language.
  • RStudio is an environment that makes working with R easier.

You need to install R before installing and using the RStudio Desktop application with it.

2. How to Download and Install R

Follow these steps to install R on a Windows computer.

Step 1: Visit the Official R Website

Open your web browser and visit the official R Project website:

https://www.r-project.org/

Step 2: Select CRAN

On the R website, locate and click the CRAN download link. CRAN stands for Comprehensive R Archive Network. It provides official R downloads and related resources.

Step 3: Select a Download Location

Choose a CRAN mirror that is convenient for you. A mirror is a server that provides the R installation files.

Step 4: Download R for Windows

If you are using Windows:

  1. Click the option for downloading R for Windows.
  2. Open the base folder.
  3. Click the download link for the latest R version.
  4. Wait for the installation file to finish downloading.

Step 5: Install R

  1. Locate the downloaded installation file.
  2. Double-click the file to start the installation.
  3. Follow the instructions displayed on your screen.
  4. Accept the licence agreement.
  5. Choose an installation location or use the default location.
  6. Continue clicking Next until the installation is complete.

Congratulations! You have installed R on your computer.

3. How to Download and Install RStudio

Now that R is installed, the next step is to install RStudio Desktop.

Step 1: Visit the Official Posit Website

Open your browser and visit:

https://posit.co/download/rstudio-desktop/

Step 2: Find RStudio Desktop

Locate the RStudio Desktop download section. Choose the free version available for your operating system.

Step 3: Download the Windows Installer

Select the Windows download option if you are using a Windows computer. Wait for the installer to download.

Step 4: Install RStudio

  1. Open the downloaded installation file.
  2. Follow the installation instructions.
  3. Choose the default installation settings if you are unsure.
  4. Complete the installation process.

Step 5: Open RStudio

Search for RStudio in your computer's Start menu and open it.

RStudio should automatically detect your installed R version. If it does not, check that R was installed correctly and that your RStudio version is compatible with it.

4. Understanding the RStudio Interface

When you open RStudio, you will see different sections called panes. Each pane performs a different task.

The main panes you should understand are:

A. Console

The Console is where you can type R commands and immediately see the results.

For example, type the following command into the Console:

10 + 20

Press Enter. R will display:

[1] 30

This means R has successfully calculated the answer.

B. Source or Script Editor

The Source pane is where you write and edit longer R programs called scripts.

Instead of typing every command directly into the Console, you can write several commands in the Script Editor, save them, and run them whenever you need them.

Example:

first_number <- 10
second_number <- 20

total <- first_number + second_number

print(total)

C. Environment Pane

The Environment pane displays objects you have created during your R session.

For example, if you create a variable called total, RStudio can display it in the Environment pane.

D. Files, Plots, Packages, and Help

These tabs provide additional tools:

  • Files: Helps you browse files and folders on your computer.
  • Plots: Displays charts and graphs created with R.
  • Packages: Helps you view and manage installed R packages.
  • Help: Provides explanations and instructions about R functions.

5. Writing Your First R Command

Now that you understand the RStudio interface, it is time to write your first R command.

Follow these steps:

  1. Open RStudio.
  2. Click inside the Console.
  3. Type the command below.
  4. Press Enter.
print("Hello, World!")

Expected result:

[1] "Hello, World!"

Well done! You have written and executed your first R command.

6. Performing Simple Calculations in R

R can work like a calculator. You can use it to perform mathematical operations.

Operation R Command Result
Addition10 + 515
Subtraction10 - 55
Multiplication10 * 550
Division10 / 52
Power2 ^ 38

Try typing these commands into your RStudio Console and observe the results.

7. Creating and Running an R Script

An R script is a file that contains R commands. It allows you to save your work so you can use it again later.

Follow these steps to create your first script:

  1. Open RStudio.
  2. Click File on the top menu.
  3. Select New File.
  4. Choose R Script.
  5. A new Script Editor window will appear.
  6. Type the following code into the editor.
# My first R script

name <- "Ade"
age <- 13

print(name)
print(age)

To run a line of code, place your cursor on that line and click Run. You can also use the keyboard shortcut Ctrl + Enter on Windows.

To run several selected lines, highlight them and click Run.

8. How to Save an R Script

Saving your script helps you avoid losing your work.

Follow these steps:

  1. Click File.
  2. Select Save As.
  3. Choose a folder where you want to save the file.
  4. Give your file a name, such as my_first_script.R.
  5. Click Save.

R scripts usually use the .R file extension.

You can open the saved file later and continue working on your program.

9. Understanding Comments in R

A comment is a note written inside your code to explain what the code does.

Comments help you and other programmers understand your work. R does not execute comments as commands.

In R, the hash symbol (#) is used to write a comment.

Example:

# This is my first comment

# Calculate the total sales
sales <- 50000 + 30000

print(sales)

R ignores the comment lines and executes the actual commands.

10. Common Problems Beginners May Face

When installing and using RStudio, you may experience some problems. Here are common examples and how to solve them.

Problem 1: RStudio Cannot Find R

Solution: Check that R is installed correctly. If necessary, reinstall R and restart RStudio.

Problem 2: A Command Produces an Error

Solution: Check your spelling, brackets, quotation marks, and symbols. R commands must follow the correct syntax.

Problem 3: You Cannot Find Your Saved Script

Solution: Check the folder you selected when saving the file. Give your scripts clear names to make them easier to locate.

Problem 4: RStudio Is Not Responding Properly

Solution: Close and reopen RStudio. Also check whether your computer has enough available memory and whether your software is up to date.

11. Practical Exercise

It is time to practise what you have learned.

Open RStudio and complete the following activities:

  1. Write a command that displays the words "Welcome to R Programming".
  2. Use R to add 150 and 250.
  3. Use R to multiply 12 by 8.
  4. Create a variable called student_name and assign it the value "Chika".
  5. Create another variable called score and assign it the value 85.
  6. Display both variables using the print() function.
  7. Create a new R script and save it as practice_module_two.R.

Sample solution:

print("Welcome to R Programming")

150 + 250

12 * 8

student_name <- "Chika"
score <- 85

print(student_name)
print(score)

12. Module Summary

In this module, you learned:

  • The difference between R and RStudio.
  • How to download and install R.
  • How to download and install RStudio Desktop.
  • How to navigate the RStudio interface.
  • How to use the Console and Script Editor.
  • How to write and execute basic R commands.
  • How to create, save, and run an R script.
  • How to write comments in R.

What Is Next?

In Module 3, we will explore R Programming Basics. You will learn about R syntax, variables, data assignment, mathematical operations, text values, and how to write simple instructions that help R perform different tasks.

Keep practising! The more commands you write, the more comfortable you will become with R programming.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’