Have you ever wondered how scientists find patterns in large amounts of data? Or how apps suggest your favourite songs and movies? The answer is data science, and one of the best tools for it is R programming.
R is a free and easy-to-learn programming language. It was specially built for working with data, numbers, and charts. Just like a calculator helps you solve math problems, R helps data scientists solve real-world problems using data.
In this tutorial, you will learn R programming completely from scratch. You do not need any previous coding experience. We will go step by step, starting from installation, all the way to building your first machine learning model.
What Is R Programming?
R programming is a free, open-source programming language. It was specially created for statistics, data analysis, and data visualization. Think of it like a super-smart calculator that can also draw beautiful graphs and find hidden patterns in large amounts of data.
R was first created in the 1990s by Ross Ihaka and Robert Gentleman at the University of Auckland. Today, millions of data scientists, researchers, and statisticians use R all over the world.
Here is why R is so special compared to other languages:
- It is completely free to download and use.
- It has thousands of ready-made packages (tools) you can add.
- It is great at making beautiful charts and graphs.
- It is the first choice of statisticians and data analysts worldwide.
- It works on Windows, Mac, and Linux computers.
Why Should You Learn R for Data Science?
Imagine you have a big box of LEGO bricks. Each brick is a piece of data. R is the set of instructions that helps you put those bricks together to build something meaningful - like a report, a chart, or a prediction.
Here are the top reasons to learn R for data science:
- Data Manipulation - R helps you clean, sort, and organize messy data quickly.
- Data Visualization - You can make line graphs, bar charts, pie charts, and scatter plots with just a few lines of code.
- Statistical Analysis - R was built for statistics, so it handles math operations very easily.
- Machine Learning - R has packages like caret, randomForest, and rpart for building prediction models.
- Big Community - Millions of users share free tutorials, code, and help online.
- Used in Top Companies - Companies like Google, Facebook, and Airbnb use R for data analysis.
Step 1 - Installing R and RStudio
Before you write your first line of R code, you need to install two things:
Installing R
- Go to the official R website: https://cran.r-project.org.
- Look for the section called "Download and Install R."
- Click the link for your operating system (Windows, Mac, or Linux).
- Select the latest release version.
- Download the file and open it.
- Follow the simple on-screen instructions, leaving all settings at the default.
Installing RStudio
R is the engine. RStudio is the car dashboard - it makes R much easier and nicer to use.
- Go to https://posit.co/download/rstudio-desktop/.
- Download the free RStudio Desktop version.
- Install it just like any other software on your computer.
- Open RStudio after installation.
Understanding the RStudio Interface
When you open RStudio for the first time, you will see four main sections:
- Console (bottom-left) - This is where you type R commands and see results immediately.
- Script Editor (top-left) - This is where you write and save longer programs.
- Environment (top-right) - This shows all your variables and data stored in memory.
- Files/Plots/Packages (bottom-right) - This shows your files, charts, installed packages, and help pages.
Tip for Beginners: Think of the Console as a chat box where you talk to R. You type a question (a command), and R gives you an answer (the result).
Step 2 - Your First R Program
Let's write your very first R program! Click on the Console in RStudio and type this:
| print("Hello, World!") |
Press Enter. You will see:
"Hello, World!"
Congratulations! You just ran your first R program. Now let's try a simple math calculation:
| 5 + 10 |
Output:
15
R can do all basic math operations:
10 + 5 # Addition → 15
10 - 3 # Subtraction → 7
4 * 6 # Multiplication → 24
20 / 4 # Division → 5
2 ^ 3 # Power (2 to the power 3) → 8
17 %% 5 # Remainder → 2
The # symbol is used to write comments - notes that R ignores. Always add comments to explain what your code does!
Step 3 - Variables in R
A variable is like a box where you store a value. You give the box a name, and whenever you need that value, you just use the name.
In R, you store values using the <- symbol (called the assignment operator):
| my_age <- 20my_name <- "Rahul"my_score <- 95.5 |
Now let's print them:
| print(my_age) # Output: 20print(my_name) # Output: "Rahul"print(my_score) # Output: 95.5 |
You can also do math with variables:
| length <- 10width <- 5area <- length * widthprint(area) # Output: 50 |
Simple Rule: Variable names cannot start with a number and cannot have spaces. Use underscores (_) instead of spaces. For example, use student_marks instead of student marks.
Step 4 - Data Types in R
Just like in the real world, where we have different types of things (numbers, words, yes/no answers), R also has different data types.
| Data Type | What It Means | Example |
| Numeric | Decimal or whole numbers | 25, 3.14 |
| Integer | Whole numbers only | 10L, 200L |
| Character | Text or words | "Hello", "Delhi" |
| Logical | True or False answers | TRUE, FALSE |
| Complex | Numbers with imaginary parts | 3+2i |
Here is how you create and check data types:
# Numeric vector
marks <- c(85, 90, 78, 92, 88)
# Character vector
students <- c("Amit", "Pooja", "Rahul", "Sneha")
# Logical vector
passed <- c(TRUE, TRUE, FALSE, TRUE)
The class() function tells you what type of data a variable holds. This is very useful when you are working with real datasets!
Step 5 - Data Structures in R
Data structures are ways to organize and store multiple values together. In data science, you rarely work with just one number - you work with hundreds or thousands of values at a time.
Vectors - The Most Basic Structure
A vector is a list of values of the same data type. Think of it like a column in an Excel sheet:
# Numeric vector
marks <- c(85, 90, 78, 92, 88)
# Character vector
students <- c("Amit", "Pooja", "Rahul", "Sneha")
# Logical vector
passed <- c(TRUE, TRUE, FALSE, TRUE)
The c() function combines values into a vector. You can do math on entire vectors at once:
marks + 5 # Adds 5 to every mark
mean(marks) # Calculates average → 86.6
max(marks) # Finds highest mark → 92
min(marks) # Finds lowest mark → 78
length(marks) # Counts elements → 5
Matrices - Like a Table of Numbers
A matrix is a two-dimensional table with rows and columns. All values must be of the same data type:
| my_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)print(my_matrix) |
Output:
| [,1] [,2] [,3][1,] 1 3 5[2,] 2 4 6 |
Data Frames - The Most Important Structure for Data Science
A data frame is like an Excel spreadsheet - it has rows and columns, and each column can hold a different data type. This is the structure you will use the most in data science!
student_data <- data.frame(
Name = c("Amit", "Pooja", "Rahul", "Sneha"),
Age = c(20, 21, 19, 22),
Marks = c(85, 90, 78, 92),
Passed = c(TRUE, TRUE, FALSE, TRUE)
)
print(student_data)
Output:
| Name Age Marks Passed1 Amit 20 85 TRUE2 Pooja 21 90 TRUE3 Rahul 19 78 FALSE4 Sneha 22 92 TRUE |
You can access specific columns using the $ sign:
Name Age Marks Passed
1 Amit 20 85 TRUE
2 Pooja 21 90 TRUE
3 Rahul 19 78 FALSE
4 Sneha 22 92 TRUE
Lists - A Flexible Container
A list can hold different types of data together - numbers, text, vectors, even other lists:
| my_list <- list( name = "Rahul", age = 20, marks = c(85, 90, 78))print(my_list$name) # Output: "Rahul" |
Step 6 - Control Structures in R
Control structures let your program make decisions and repeat tasks - just like how you decide what to wear based on the weather, or how you repeat brushing your teeth every morning.
If-Else Statements (Making Decisions)
| marks <- 75 if (marks >= 60) { print("You Passed!")} else { print("You Failed. Try again!")} |
Output:
"You Passed!"
You can also add more conditions using else if:
marks <- 85
if (marks >= 90) {
print("Grade: A+")
} else if (marks >= 80) {
print("Grade: A")
} else if (marks >= 70) {
print("Grade: B")
} else {
print("Grade: C")
}
Output:
"Grade: A"
For Loop (Repeating a Task)
A for loop lets you repeat a block of code multiple times without writing the same line again and again:
| for (i in 1:5) { print(paste("This is line number", i))} |
Output:
"This is line number 1"
"This is line number 2"
"This is line number 3"
"This is line number 4"
"This is line number 5"
While Loop (Repeat Until Done)
| count <- 1 while (count <= 5) { print(count) count <- count + 1} |
Output:
1
2
3
4
5
Step 7 - Functions in R
A function is a block of code that does a specific job. You write it once, and then you can use it as many times as you want. Think of it like a recipe - you write the recipe once and cook the dish whenever you want!
Using Built-In Functions
R comes with hundreds of ready-made functions:
numbers <- c(10, 20, 30, 40, 50)
sum(numbers) # Total → 150
mean(numbers) # Average → 30
median(numbers) # Middle value → 30
sd(numbers) # Standard deviation → 15.81
var(numbers) # Variance → 250
sqrt(144) # Square root → 12
abs(-45) # Absolute value → 45
Writing Your Own Functions
# Function to calculate area of a rectangle
calculate_area <- function(length, width) {
area <- length * width
return(area)
}
# Call the function
result <- calculate_area(10, 5)
print(result) # Output: 50
You can also set default values for function arguments:
greet_student <- function(name, message = "Welcome to R Programming!") {
print(paste("Hello", name, "-", message))
}
greet_student("Rahul")
# Output: "Hello Rahul - Welcome to R Programming!"
greet_student("Pooja", "You are doing great!")
# Output: "Hello Pooja - You are doing great!"
Step 8 - R Packages
A package is a collection of extra tools (functions and datasets) that someone has already built for you. Installing a package is like downloading a new app on your phone - it adds new abilities to R.
How to Install and Load a Package
| # Install a package (do this only once)install.packages("ggplot2") # Load the package (do this every time you open R)library(ggplot2) |
The Most Important Packages for Data Science
| Package | What It Does |
| ggplot2 | Creates beautiful visualizations and charts |
| dplyr | Makes data manipulation easy and fast |
| tidyr | Helps clean and reshape messy data |
| readr | Reads CSV and other data files quickly |
| caret | Builds and evaluates machine learning models |
| randomForest | Creates Random Forest prediction models |
| rpart | Builds Decision Tree models |
| lubridate | Makes working with dates and times easy |
The combination of ggplot2, dplyr, and tidyr is called the Tidyverse and is the most widely used toolkit in data science with R.
Step 9 - Data Visualization With ggplot2
One of the biggest strengths of R is its ability to make professional, beautiful charts. The ggplot2 package is the most popular tool for this.
A Basic Line Chart
library(ggplot2)
# Sample data
months <- c(1, 2, 3, 4, 5, 6)
sales <- c(200, 350, 300, 450, 400, 500)
data <- data.frame(Month = months, Sales = sales)
ggplot(data, aes(x = Month, y = Sales)) +
geom_line(color = "blue", size = 1.2) +
geom_point(color = "red", size = 3) +
ggtitle("Monthly Sales Data") +
xlab("Month") +
ylab("Sales")
A Bar Chart
| subjects <- c("Math", "Science", "English", "History", "Art")scores <- c(90, 85, 78, 88, 95) subject_data <- data.frame(Subject = subjects, Score = scores) ggplot(subject_data, aes(x = Subject, y = Score, fill = Subject)) + geom_bar(stat = "identity") + ggtitle("Student Scores by Subject") + xlab("Subject") + ylab("Score") + theme_minimal() |
A Histogram
# Generate random student marks
set.seed(42)
student_marks <- rnorm(100, mean = 75, sd = 10)
ggplot(data.frame(Marks = student_marks), aes(x = Marks)) +
geom_histogram(binwidth = 5, fill = "skyblue", color = "black") +
ggtitle("Distribution of Student Marks") +
xlab("Marks") +
ylab("Number of Students")
How ggplot2 Works: Think of it like building a painting layer by layer. First, you set up the canvas (ggplot()), then you add the type of chart (geom_line, geom_bar), and then you add decorations like titles and colors.
Step 10 - Data Manipulation With dplyr
In real data science projects, data is rarely clean and ready to use. You will almost always need to filter, sort, rename, or summarize your data first. The dplyr package makes all of this very easy.
| library(dplyr) # Sample datasetstudents <- data.frame( Name = c("Amit", "Pooja", "Rahul", "Sneha", "Vikram"), Age = c(20, 21, 19, 22, 20), Marks = c(85, 90, 60, 92, 75), City = c("Delhi", "Mumbai", "Delhi", "Pune", "Mumbai")) |
Filter - Select Specific Rows
| # Show only students who scored more than 80top_students <- filter(students, Marks > 80)print(top_students) |
Select - Pick Specific Columns
| # Show only Name and Marks columnsname_marks <- select(students, Name, Marks)print(name_marks) |
Arrange - Sort the Data
| # Sort students by marks (highest first)sorted_students <- arrange(students, desc(Marks))print(sorted_students) |
Mutate - Add a New Column
# Add a Grade column based on marks
students <- mutate(students,
Grade = ifelse(Marks >= 90, "A+",
ifelse(Marks >= 80, "A",
ifelse(Marks >= 70, "B", "C")))
)
print(students)
Summarise - Get Summary Statistics
| # Calculate average marks by citycity_summary <- students %>% group_by(City) %>% summarise( Average_Marks = mean(Marks), Total_Students = n() )print(city_summary) |
The %>% symbol is called the pipe operator. It passes the output of one function directly into the next function - like a water pipe connecting two tanks!
Step 11 - Reading Real Data Files
In real data science, you will work with data stored in files like CSV (Comma-Separated Values) files. R can read these files very easily:
# Read a CSV file
my_data <- read.csv("student_data.csv")
# See the first 6 rows
head(my_data)
# See the last 6 rows
tail(my_data)
# Get a quick summary of all columns
summary(my_data)
# Check the size (rows and columns)
dim(my_data)
# Check column names
colnames(my_data)
# Check data types of all columns
str(my_data)
Writing Data to a File
| # Save your data frame as a CSV filewrite.csv(students, "cleaned_students.csv", row.names = FALSE) |
Step 12 - Handling Missing Data
In real-world datasets, some values will be missing (empty). In R, missing values are represented as NA (Not Available). Knowing how to handle them is a crucial data science skill.
data_with_na <- c(10, 20, NA, 40, NA, 60)
# Check which values are missing
is.na(data_with_na)
# Output: FALSE FALSE TRUE FALSE TRUE FALSE
# Count missing values
sum(is.na(data_with_na))
# Output: 2
# Calculate mean ignoring NA values
mean(data_with_na, na.rm = TRUE)
# Output: 32.5
# Replace NA with the mean value
data_with_na[is.na(data_with_na)] <- mean(data_with_na, na.rm = TRUE)
print(data_with_na)
# Output: 10 20 32.5 40 32.5 60
Step 13 - Simple Machine Learning in R
Now comes the exciting part! Machine learning means teaching a computer to learn from data and make predictions. R has excellent tools for this.
Linear Regression - Predicting Numbers
Linear Regression helps you predict a number based on other numbers. For example, predicting a student's final exam score based on their practice test scores:
# Sample data
study_hours <- c(1, 2, 3, 4, 5, 6, 7, 8)
exam_scores <- c(40, 50, 55, 65, 70, 75, 85, 90)
study_data <- data.frame(Hours = study_hours, Score = exam_scores)
# Build the linear regression model
model <- lm(Score ~ Hours, data = study_data)
summary(model)
# Predict score for a student who studied 9 hours
new_data <- data.frame(Hours = 9)
predicted_score <- predict(model, newdata = new_data)
print(predicted_score) # Output: approximately 96
Decision Tree - Making Decisions From Data
| library(rpart) # Sample data: Will a student pass based on study hours and attendance?student_train <- data.frame( Study_Hours = c(8, 3, 5, 1, 7, 2, 6, 4), Attendance = c(90, 60, 75, 40, 85, 50, 80, 65), Pass = c("Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No")) # Train the decision tree modeltree_model <- rpart(Pass ~ Study_Hours + Attendance, data = student_train, method = "class") # Predict for new studentsnew_students <- data.frame(Study_Hours = c(6, 2), Attendance = c(80, 45))predictions <- predict(tree_model, newdata = new_students, type = "class")print(predictions) |
Step 14 - Exploratory Data Analysis (EDA)
Exploratory Data Analysis (EDA) means carefully exploring a dataset to understand what it contains before building any models. It is like reading all the ingredients on a food packet before you start cooking.
Here is a complete EDA workflow using R's built-in mtcars dataset (a dataset about cars):
# Load the built-in dataset
data(mtcars)
# 1. See the first few rows
head(mtcars)
# 2. Check dataset size
dim(mtcars) # 32 rows, 11 columns
# 3. Check data types
str(mtcars)
# 4. Statistical summary
summary(mtcars)
# 5. Check for missing values
sum(is.na(mtcars)) # Output: 0 (no missing values)
# 6. Correlation between variables
cor(mtcars[, c("mpg", "hp", "wt")])
# 7. Visualize the distribution of mpg (miles per gallon)
library(ggplot2)
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth = 3, fill = "coral", color = "black") +
ggtitle("Distribution of MPG") +
xlab("Miles Per Gallon") +
ylab("Count")
# 8. Scatter plot: weight vs mpg
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "blue", size = 3) +
geom_smooth(method = "lm", color = "red") +
ggtitle("Car Weight vs Miles Per Gallon") +
xlab("Weight (1000 lbs)") +
ylab("Miles Per Gallon")
The R Learning Roadmap
Here is a simple step-by-step path you should follow to become confident in R for data science:
- Week 1-2: Learn basic syntax, variables, data types, and operators
- Week 3-4: Master vectors, matrices, data frames, and lists
- Week 5-6: Practice control structures (if-else, loops) and writing functions
- Week 7-8: Learn dplyr for data manipulation and ggplot2 for visualization
- Week 9-10: Practice reading CSV files and handling missing data
- Week 11-12: Try simple machine learning models (linear regression, decision trees)
- Week 13+: Work on real projects using Kaggle datasets or your own data
Top Free Resources to Learn R
Here are the best places to continue your R learning journey for free:
- RStudio Cloud Primers - Learn directly in your browser without installing anything (cloud.rstudio.com)
- W3Schools R Tutorial - Simple, clear tutorials for beginners (w3schools.com/r)
- DataCamp's RStudio Tutorial - A complete beginner's guide updated for 2026
- YouTube - Search "R for beginners" for hundreds of free video tutorials
Quick Reference: Most Used R Commands
| Command | What It Does |
| print(x) | Prints the value of x |
| class(x) | Shows the data type of x |
| str(x) | Shows the structure of a dataset |
| summary(x) | Shows a statistical summary |
| head(x) | Shows first 6 rows |
| dim(x) | Shows the number of rows and columns count |
| is.na(x) | Checks for missing values |
| mean(x) | Calculates average |
| c() | Combines values into a vector |
| data.frame() | Creates a data frame |
| install.packages() | Installs a new package |
| library() | Loads an installed package |
Conclusion
R programming is a wonderful skill to have in the world of data science. You have now learned the most important foundations - from installing R and RStudio, to writing your first program, working with data structures, making beautiful visualizations, manipulating data, and even building your first machine learning model. The best way to truly master R is to practice every single day - even just 30 minutes of coding can make a huge difference over a few months. Start with small projects, explore free datasets, and most importantly, enjoy the process of turning raw data into meaningful insights.