Curriculum
Course: Batch 134: Data Analysis and Visualizat...
Login
Text lesson

1: Introduction to R and RStudio

Section 1: Introduction to R and RStudio

In this section, you will learn the fundamentals of R and RStudio, which are among the most powerful tools for statistical computing, data analysis, machine learning, and data visualization. By the end of this section, you will be familiar with the R programming environment, understand the purpose of RStudio, and be able to perform basic operations within the R console.

Learning Objectives

After completing this section, you will be able to:

  • Understand what R is and why it is widely used in data science, statistics, and research.
  • Understand the role of RStudio as an Integrated Development Environment (IDE) for R.
  • Install and configure R and RStudio on your computer.
  • Explore the RStudio interface, including:
    • Console
    • Script Editor (Source)
    • Environment
    • History
    • Files
    • Plots
    • Packages
    • Help
    • Viewer
  • Create, save, and organize R scripts.
  • Run individual lines of code and complete scripts.
  • Understand the R working directory and project management.
  • Install, update, and load R packages.
  • Access built-in help files and documentation.
  • Perform simple calculations and execute your first R commands.
  • Learn basic coding practices and shortcuts that improve productivity.

Topics Covered

  • What is R?
  • Applications of R in Data Science and Research
  • Why Use R?
  • Installing R
  • Installing RStudio
  • Understanding the RStudio Interface
  • Creating Your First R Script
  • Running R Code
  • Installing and Loading Packages
  • Using Help and Documentation
  • Setting the Working Directory
  • Creating an R Project
  • Basic R Commands and Calculations
  • Saving and Organizing Your Work
  • Best Practices for Beginners

Practical Exercises

At the end of this section, you will be able to:

  1. Install R and RStudio successfully.
  2. Open RStudio and identify each panel.
  3. Create and save your first R script.
  4. Execute simple mathematical calculations.
  5. Create variables and display their values.
  6. Install and load an R package.
  7. Use the Help system to find documentation.
  8. Create an R Project and organize your files.

Expected Learning Outcome

Upon successful completion of this section, you will have a solid understanding of the R programming environment and RStudio interface. You will be prepared to write, execute, and manage R scripts efficiently, providing a strong foundation for the advanced topics covered in subsequent sections of the course, including data manipulation, visualization, statistical analysis, and machine learning.

 
 
content of this lesson
 

Lesson 1: Introduction to R and RStudio

1.1 What is R?

R is a free, open-source programming language and software environment designed for statistical computing, data analysis, machine learning, and graphical visualization. It was developed by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and has become one of the most widely used programming languages in academia, research, healthcare, finance, business analytics, and artificial intelligence.

R provides thousands of built-in functions and more than 20,000 packages contributed by the global R community through the Comprehensive R Archive Network (CRAN). These packages extend R’s capabilities to perform advanced statistical analyses, create publication-quality graphics, develop predictive models, and analyze large datasets.

Today, R is widely used by:

  • Data Scientists
  • Data Analysts
  • Biostatisticians
  • Researchers
  • Business Analysts
  • Machine Learning Engineers
  • Bioinformaticians
  • Epidemiologists
  • Financial Analysts

1.2 Why Learn R?

R has become one of the most important programming languages for data analysis because it combines statistical power with flexibility.

Advantages of R

  • Free and Open Source
  • Cross-platform (Windows, macOS, Linux)
  • Excellent statistical capabilities
  • Publication-quality graphics
  • Large community support
  • Thousands of packages
  • Strong machine learning ecosystem
  • Excellent reproducibility
  • Integration with Python, SQL, and Excel
  • Widely accepted in research and industry

1.3 Applications of R

R is used in almost every field involving data.

Healthcare

  • Clinical trials
  • Epidemiology
  • Biostatistics
  • Public health

Agriculture

  • Crop analysis
  • Plant breeding
  • Soil science
  • Precision agriculture

Business

  • Sales forecasting
  • Customer analytics
  • Marketing analysis
  • Financial reporting

Machine Learning

  • Classification
  • Regression
  • Clustering
  • Deep learning

Bioinformatics

  • RNA-Seq analysis
  • Genomics
  • Transcriptomics
  • Proteomics

Finance

  • Risk analysis
  • Portfolio optimization
  • Stock market prediction

1.4 What is RStudio?

RStudio is an Integrated Development Environment (IDE) for R.

Think of R as the engine, while RStudio is the dashboard that makes using R easier and more efficient.

RStudio provides a user-friendly interface that helps you write, organize, run, debug, and manage R code.


1.5 Why Use RStudio?

Instead of using the basic R console, most professionals use RStudio because it offers:

  • Syntax highlighting
  • Code completion
  • Script editor
  • Package management
  • Plot viewer
  • Data viewer
  • Integrated Help system
  • Project management
  • Version control support
  • Easy debugging tools

1.6 Installing R

Step 1

Visit the official CRAN website:

https://cran.r-project.org

Step 2

Choose your operating system:

  • Windows
  • macOS
  • Linux

Step 3

Download the latest stable version.

Step 4

Run the installer using the default settings.


1.7 Installing RStudio

Download RStudio Desktop from:

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

Install RStudio after installing R.

Important: R must be installed before RStudio.


1.8 Understanding the RStudio Interface

When you open RStudio, you will see four main panels.

Source (Top Left)

Used to write and save R scripts.

Example:

 
x <- 10
y <- 20
x + y
 

Console (Bottom Left)

The Console executes R commands immediately.

Example:

 
5 + 10
 

Output:

 
15
 

Environment (Top Right)

Displays all variables, datasets, functions, and objects currently loaded into memory.

Example:

 
x = 10
y = 20
 

Files, Plots, Packages, Help, Viewer (Bottom Right)

Files

Browse folders.

Plots

Display graphs.

Packages

Install and load packages.

Help

Access documentation.

Viewer

Display HTML content and interactive applications.


1.9 Creating Your First R Script

Click:

File → New File → R Script

Write:

 
print("Hello, World!")
 

Save the file as:

 
Lesson1.R
 

1.10 Running Code

Run one line:

Ctrl + Enter

Run the entire script:

Ctrl + Shift + Enter


1.11 Your First Commands

Addition

 
5 + 6
 

Subtraction

 
20 - 4
 

Multiplication

 
5 * 7
 

Division

 
20 / 4
 

Exponent

 
5^2
 

Square Root

 
sqrt(25)
 

1.12 Creating Variables

Variables store data for later use.

Example:

 
age <- 25

height <- 170

weight <- 70
 

Display a variable:

 
age
 

Output:

 
25
 

1.13 Comments in R

Comments help explain your code and are ignored during execution.

 
# This is a comment

x <- 100
 

1.14 Installing Packages

Packages extend R’s functionality.

Install a package:

 
install.packages("ggplot2")
 

Load the package:

 
library(ggplot2)
 

1.15 Getting Help

Use:

 
?mean
 

or

 
help(mean)
 

Search documentation:

 
??regression
 

1.16 Working Directory

Check the current working directory:

 
getwd()
 

Set a working directory:

 
setwd("C:/Users/YourName/Documents/RProject")
 

1.17 Creating an R Project

Creating an R Project keeps your scripts, data, outputs, and results organized.

To create a new project:

  1. Go to File → New Project.
  2. Choose New Directory or Existing Directory.
  3. Specify the project folder and create the project.

Using projects helps avoid issues with file paths and improves reproducibility.


1.18 Best Practices

  • Save your work regularly.
  • Organize files into folders (e.g., Data, Scripts, Figures, Results).
  • Use meaningful variable names.
  • Comment your code.
  • Keep one project per analysis.
  • Install packages only once, but load them in every session.
  • Write clean, readable, and reproducible code.