🎓 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 (178K+ 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'.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment