How to Remove Multiple Rows in R (With Examples) - Statology (2024)

You can use one of the following methods to remove multiple rows from a data frame in R:

Method 1: Remove Specific Rows

#remove rows 2, 3, and 4new_df <- df[-c(2, 3, 4), ]

Method 2: Remove Range of Rows

#remove rows 2 through 5new_df <- df[-c(2:5), ]

Method 3: Remove Last N Rows

#remove rows 4 through last rownew_df <- df[-c(4:nrow(df)), ]

The following examples show how to use each of these methods in practice with the following data frame:

#create data framedf <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F'), points=c(99, 90, 86, 88, 95, 99), assists=c(33, 28, 31, 39, 34, 24))#view data framedf team points assists1 A 99 332 B 90 283 C 86 314 D 88 395 E 95 346 F 99 24

Example 1: Remove Specific Rows

The following code shows how to remove rows 2, 3, and 4 from the data frame:

#define new data frame with rows 2, 3, 4 removednew_df <- df[-c(2, 3, 4),]#view new data framenew_df team points assists1 A 99 335 E 95 346 F 99 24

Notice that rows 2, 3, and 4 have all been removed from the data frame.

Example 2: Remove Range of Rows

The following code shows how to remove rows in the range of 2 through 5:

#define new data frame with rows 2 through 5 removednew_df <- df[-c(2:5),]#view new data framenew_df team points assists1 A 99 336 F 99 24

Notice that rows 2, 3, 4, and 5 have been removed.

Example 3: Remove Last N Rows

The following code shows how to remove rows 4 through the last row:

#remove rows 4 through last rownew_df <- df[-c(4:nrow(df)), ]#view new data framenew_df team points assists1 A 99 332 B 90 283 C 86 31

Notice that row 4 and all rows after it have been removed.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Remove Duplicate Rows in R
How to Count Number of Rows in R
How to Remove Rows with Some or All NAs in R

I am an expert in R programming with a deep understanding of data manipulation and analysis. My expertise is demonstrated by a thorough knowledge of the methods used in the article you provided, as well as a broader understanding of related concepts in R programming.

Now, let's delve into the concepts mentioned in the article:

  1. Data Frame Creation: The article starts by creating a data frame named df using the data.frame function. The data frame consists of columns such as "team," "points," and "assists," and it looks like this:

    df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F'), points=c(99, 90, 86, 88, 95, 99), assists=c(33, 28, 31, 39, 34, 24))
  2. Method 1: Remove Specific Rows: The first method demonstrates how to remove specific rows from the data frame using negative indexing. Rows 2, 3, and 4 are removed, and the resulting data frame is stored in new_df:

    new_df <- df[-c(2, 3, 4), ]
  3. Method 2: Remove Range of Rows: The second method shows how to remove a range of rows (rows 2 through 5) from the data frame:

    new_df <- df[-c(2:5), ]
  4. Method 3: Remove Last N Rows: The third method involves removing the last N rows from the data frame. In this case, rows 4 through the last row are removed:

    new_df <- df[-c(4:nrow(df)), ]
  5. Examples and Output: The article provides examples for each method along with the resulting data frames (new_df). It shows how specific rows or a range of rows can be removed, and it includes the resulting data frames after the removal operations.

  6. Additional Resources: The article concludes by mentioning additional resources or tutorials related to common operations in R, such as removing duplicate rows, counting the number of rows, and removing rows with missing values (NAs).

As an expert, I can assure you that these methods are commonly used in R for data manipulation tasks, and they showcase the flexibility and power of R in handling data frames efficiently. If you have any specific questions or need further clarification on these concepts, feel free to ask.

How to Remove Multiple Rows in R (With Examples) - Statology (2024)
Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6100

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.