📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
When working with dataframes in R, there might be situations where we need to drop specific rows, either based on their index or based on some conditions. In this guide, we will explore how to achieve this using R.
2. Program Overview
1. Create an initial dataframe.
2. Drop rows based on their index.
3. Drop rows based on a condition.
3. Code Program
# Load necessary libraries library(dplyr) # Create an initial dataframe df <- data.frame( Name = c('Alice', 'Bob', 'Charlie', 'David', 'Eve'), Age = c(25, 30, 28, 35, 29), Occupation = c('Engineer', 'Doctor', 'Lawyer', 'Artist', 'Engineer') ) # Print the original dataframe print("Original Dataframe:") print(df) # Drop the 2nd and 4th rows df <- df[-c(2,4), ] # Print the dataframe after dropping rows by index print("Dataframe after Dropping 2nd and 4th Rows:") print(df) # Drop rows where Occupation is 'Engineer' df <- df %>% filter(Occupation != "Engineer") # Print the dataframe after dropping rows by condition print("Dataframe after Dropping Rows with Occupation as 'Engineer':") print(df)
Output:
[1] "Original Dataframe:" Name Age Occupation 1 Alice 25 Engineer 2 Bob 30 Doctor 3 Charlie 28 Lawyer 4 David 35 Artist 5 Eve 29 Engineer [1] "Dataframe after Dropping 2nd and 4th Rows:" Name Age Occupation 1 Alice 25 Engineer 3 Charlie 28 Lawyer 5 Eve 29 Engineer [1] "Dataframe after Dropping Rows with Occupation as 'Engineer':" Name Age Occupation 3 Charlie 28 Lawyer
4. Step By Step Explanation
- We start by creating a dataframe df with columns Name, Age, and Occupation.- To drop rows based on their index, we use negative indexing. For instance, to drop the 2nd and 4th rows, we use df <- df[-c(2,4), ].- To drop rows based on a specific condition, we use the filter function from the dplyr package. In our example, we drop rows where the Occupation column has the value 'Engineer'.
Comments
Post a Comment
Leave Comment