Introduction
Filtering rows in a data frame is a common task in data analysis, allowing you to focus on specific subsets of data that meet certain conditions. This guide will walk you through writing an R program to filter rows in a data frame based on specific criteria.
Problem Statement
Create an R program that:
- Creates an initial data frame.
- Filters the data frame based on a specific condition.
- Displays the filtered data frame.
Example:
- Input: A data frame with
Name,Age,Gender, andScorecolumns. Filter the rows whereScoreis greater than 85. - Output: A filtered data frame containing only rows where
Scoreis greater than 85.
Solution Steps
- Create the Initial Data Frame: Use the
data.frame()function to create a data frame. - Filter the Data Frame: Use a logical condition to filter the rows of the data frame.
- Display the Filtered Data Frame: Use the
print()function to display the filtered data frame.
R Program
# R Program to Filter Rows in a Data Frame
# Author: Ramesh Fadatare
# Step 1: Create the initial data frame
names <- c("Ramesh", "Suresh", "Mahesh", "Ganesh", "Rajesh")
ages <- c(25, 30, 22, 28, 24)
genders <- c("Male", "Male", "Male", "Male", "Male")
scores <- c(85.5, 90.0, 88.5, 92.0, 80.0)
students_data <- data.frame(Name = names, Age = ages, Gender = genders, Score = scores)
# Step 2: Filter the data frame where 'Score' is greater than 85
filtered_data <- students_data[students_data$Score > 85, ]
# Step 3: Display the filtered data frame
print("Filtered Data Frame (Score > 85):")
print(filtered_data)
Explanation
Step 1: Create the Initial Data Frame
- The data frame
students_datais created using thedata.frame()function, with columns forName,Age,Gender, andScore.
Step 2: Filter the Data Frame
- The data frame is filtered using the condition
students_data$Score > 85, which selects only the rows where theScorecolumn is greater than 85. - The filtered data is stored in the
filtered_datavariable.
Step 3: Display the Filtered Data Frame
- The
print()function is used to display the filtered data frame, showing only the rows that meet the specified condition.
Output Example
Example:
[1] "Filtered Data Frame (Score > 85):"
Name Age Gender Score
2 Suresh 30 Male 90.0
3 Mahesh 22 Male 88.5
4 Ganesh 28 Male 92.0
Conclusion
This R program demonstrates how to filter rows in a data frame based on specific conditions. It covers essential operations such as creating a data frame, applying a logical condition to filter rows, and displaying the filtered data. Filtering is a critical operation in data analysis, allowing you to focus on relevant subsets of data for further analysis. This example is particularly useful for anyone learning how to manipulate and analyze data frames in R.