Data Manipulation with Base R

📘 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

Base R offers powerful functions for selecting rows and columns, filtering data, sorting, merging datasets, creating new variables, and summarizing information efficiently.

đŸŽ¯ 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

Data Manipulation
Select Data
Filter Rows
Sort Data
Add or Remove Columns
Merge Data
Summarize Data

📝 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

Import Data
Inspect Dataset
Clean and Transform Data
Filter and Sort
Create New Variables
Summarize Results

📋 Frequently Used Base R Functions

FunctionPurpose
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

MistakeExplanationSolution
Selecting incorrect row or column indexesMay return unexpected data.Verify indexes or use column names.
Using merge() without a common keyProduces incorrect combinations.Specify the correct by argument.
Ignoring missing valuesCan affect summaries and calculations.Handle missing values before analysis.
Overwriting original data accidentallyMay 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

Effective data manipulation is the foundation of data analysis. Clean, organized, and well-structured data leads to more accurate insights and reliable analytical results.

📝 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.

>>"Well-organized data is the first step toward meaningful analysis and informed decision-making."