đ Introduction
Data manipulation is the process of organizing, modifying, transforming, filtering, and summarizing data to prepare it for analysis. Base R provides a rich collection of built-in functions for performing these tasks without requiring additional packages. Learning data manipulation is essential because real-world datasets often need cleaning and restructuring before meaningful analysis can be performed.
Information
đ¯ Why Learn Data Manipulation?
- Clean raw datasets.
- Prepare data for analysis.
- Filter and organize information.
- Create new calculated variables.
- Generate meaningful summaries.
đ Common Data Manipulation Tasks
đ Creating a Sample Data Frame
Sample Dataset
students <- data.frame(
ID = c(101,102,103,104),
Name = c("Alice","Bob","Charlie","David"),
Marks = c(85,72,91,78),
Age = c(20,21,20,22)
)
print(students)đ Selecting Columns
Columns can be selected using the $ operator, column names, or column indexes.
Selecting Columns
students$Name
students[, "Marks"]
students[, 2]đ Selecting Rows
Rows are selected using row indexes.
Selecting Rows
students[1, ]
students[2:3, ]đ¯ Filtering Rows
Logical conditions can be used to extract rows that satisfy specific criteria.
Filtering Data
students[
students$Marks >= 80,
]Output
Console Output
ID Name Marks Age
1 101 Alice 85 20
3 103 Charlie 91 20đ Selecting Multiple Columns
Selecting Multiple Columns
students[
,
c("Name", "Marks")
]â Adding a New Column
New variables can be added by assigning values to a new column.
Adding a Column
students$Grade <- c(
"A",
"B",
"A",
"B"
)
print(students)âī¸ Modifying Existing Data
Updating Values
students$Marks <-
students$Marks + 5
print(students)â Removing Columns
Assign NULL to remove an existing column.
Removing a Column
students$Age <- NULL
print(students)đ Sorting Data
The order() function sorts data based on one or more columns.
Sorting by Marks
students[
order(students$Marks),
]Descending Order
Sorting Descending
students[
order(-students$Marks),
]đ Renaming Columns
Changing Column Names
names(students) <- c(
"StudentID",
"StudentName",
"Marks",
"Grade"
)
print(students)đĻ Combining Data Frames
Data frames can be combined either vertically using rbind() or horizontally using cbind().
Adding Rows
newStudent <- data.frame(
ID = 105,
Name = "Emma",
Marks = 88,
Age = 21
)
rbind(
students,
newStudent
)Adding Columns
department <- data.frame(
Department = c(
"IT",
"CS",
"IT",
"AI"
)
)
cbind(
students,
department
)đ Merging Data Frames
The merge() function combines two data frames based on a common column.
Merging Data Frames
studentInfo <- data.frame(
ID = c(101,102,103),
City = c(
"Mumbai",
"Delhi",
"Pune"
)
)
merge(
students,
studentInfo,
by = "ID"
)đ Summarizing Data
Base R provides several functions to summarize numerical data.
Summary Functions
summary(students)
mean(students$Marks)
max(students$Marks)
min(students$Marks)
sd(students$Marks)đ Creating New Variables
New columns can be calculated using existing variables.
Creating a New Variable
students$Result <-
ifelse(
students$Marks >= 80,
"Pass",
"Needs Improvement"
)
print(students)đ Finding Duplicate Rows
Checking Duplicates
duplicated(students)đ§š Removing Duplicate Rows
Removing Duplicates
unique(students)đ Viewing Dataset Structure
Inspecting Data
str(students)
head(students)
tail(students)
dim(students)đ Real-World Example
A company stores employee information and wants to identify employees earning more than âš60,000 while calculating the average salary.
Employee Salary Analysis
employees <- data.frame(
Name = c(
"Alice",
"Bob",
"Charlie",
"David"
),
Salary = c(
55000,
62000,
70000,
58000
)
)
highSalary <- employees[
employees$Salary > 60000,
]
averageSalary <- mean(
employees$Salary
)
print(highSalary)
print(averageSalary)đ Data Manipulation Workflow
đ Frequently Used Base R Functions
| Function | Purpose |
|---|---|
| subset() | Filters rows and selects columns. |
| order() | Sorts data. |
| merge() | Combines data frames. |
| rbind() | Adds rows. |
| cbind() | Adds columns. |
| summary() | Generates descriptive statistics. |
| head() | Displays the first rows. |
| tail() | Displays the last rows. |
| unique() | Removes duplicate rows. |
| duplicated() | Identifies duplicate rows. |
â ī¸ Common Mistakes
| Mistake | Explanation | Solution |
|---|---|---|
| Selecting incorrect row or column indexes | May return unexpected data. | Verify indexes or use column names. |
| Using merge() without a common key | Produces incorrect combinations. | Specify the correct by argument. |
| Ignoring missing values | Can affect summaries and calculations. | Handle missing values before analysis. |
| Overwriting original data accidentally | May result in data loss. | Create a copy before making major changes. |
đĄ Best Practices
- Inspect data using str() and summary() before manipulation.
- Use descriptive column names.
- Handle missing values before performing calculations.
- Create copies of important datasets before modifying them.
- Use built-in Base R functions to keep code simple and portable.
Best Practice
đ Summary
Data manipulation with Base R involves selecting, filtering, sorting, modifying, combining, and summarizing datasets using built-in functions. You learned how to work with rows and columns, create and remove variables, merge datasets, identify duplicates, inspect data structures, and generate summaries. Mastering these techniques enables you to prepare real-world datasets efficiently for analysis, visualization, and statistical modeling without relying on external packages.