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.
By the end of this module, you should be able to:
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.
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:
You need to install R before installing and using the RStudio Desktop application with it.
Follow these steps to install R on a Windows computer.
Open your web browser and visit the official R Project website:
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.
Choose a CRAN mirror that is convenient for you. A mirror is a server that provides the R installation files.
If you are using Windows:
Congratulations! You have installed R on your computer.
Now that R is installed, the next step is to install RStudio Desktop.
Open your browser and visit:
https://posit.co/download/rstudio-desktop/
Locate the RStudio Desktop download section. Choose the free version available for your operating system.
Select the Windows download option if you are using a Windows computer. Wait for the installer to download.
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.
When you open RStudio, you will see different sections called panes. Each pane performs a different task.
The main panes you should understand are:
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.
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)
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.
These tabs provide additional tools:
Now that you understand the RStudio interface, it is time to write your first R command.
Follow these steps:
print("Hello, World!")
Expected result:
[1] "Hello, World!"
Well done! You have written and executed your first R command.
R can work like a calculator. You can use it to perform mathematical operations.
| Operation | R Command | Result |
|---|---|---|
| Addition | 10 + 5 | 15 |
| Subtraction | 10 - 5 | 5 |
| Multiplication | 10 * 5 | 50 |
| Division | 10 / 5 | 2 |
| Power | 2 ^ 3 | 8 |
Try typing these commands into your RStudio Console and observe the results.
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:
# 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.
Saving your script helps you avoid losing your work.
Follow these steps:
R scripts usually use the .R file extension.
You can open the saved file later and continue working on your program.
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.
When installing and using RStudio, you may experience some problems. Here are common examples and how to solve them.
Solution: Check that R is installed correctly. If necessary, reinstall R and restart RStudio.
Solution: Check your spelling, brackets, quotation marks, and symbols. R commands must follow the correct syntax.
Solution: Check the folder you selected when saving the file. Give your scripts clear names to make them easier to locate.
Solution: Close and reopen RStudio. Also check whether your computer has enough available memory and whether your software is up to date.
It is time to practise what you have learned.
Open RStudio and complete the following activities:
Sample solution:
print("Welcome to R Programming")
150 + 250
12 * 8
student_name <- "Chika"
score <- 85
print(student_name)
print(score)
In this module, you learned:
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.