R Program to Delete Rows From Dataframe

Example 1: Delete Single Row of Dataframe in R

# Create a data frame
dataframe1 <- data.frame (
  Name = c("Juan", "Alcaraz", "Simantha"),
  Age = c(22, 15, 19),
  Address = c("Nepal", "USA", "Germany")
)

# delete 1st row
print(dataframe1[-1, ])

# extract 1st and 3rd row
print(dataframe1[-c(1,3), ])

Output

     Name Age Address
2  Alcaraz  15     USA
3 Simantha  19 Germany

In the above example, we have created a dataframe named dataframe1. And we have deleted a row using the index value and - sign.

Here, dataframe1[-1, ] deletes entire elements of 1st row


Example 2: Split Dataframe by Column Names in R

# Create a data frame
dataframe1 <- data.frame (
  Name = c("Juan", "Alcaraz", "Simantha"),
  Age = c(22, 15, 19),
  Address = c("Nepal", "USA", "Germany")
)

# delete 1st and 3rd row
print(dataframe1[-c(1,3), ])

Output

     Name Age Address
2 Alcaraz  15     USA

Here, dataframe1[-c(1,3), ] deletes entire elements of 1st and 3rd row.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges