How to Remove First Row from Data Frame in R (2 Examples)


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

Method 1: Use Base R

df <- df[-1, ]

Method 2: Use dplyr package

library(dplyr)

df <- df %>% slice(-1)

The following examples show how to use each method in practice.

Example 1: Remove First Row Using Base R

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(team=c(NA, 'A', 'B', 'C', 'D', 'E'),
                 points=c(NA, 99, 90, 86, 88, 95),
                 assists=c(NA, 33, 28, 31, 39, 34),
                 rebounds=c(NA, 30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1 <NA>     NA      NA       NA
2    A     99      33       30
3    B     90      28       28
4    C     86      31       24
5    D     88      39       24
6    E     95      34       28

We can use the following code to remove the first row from the data frame:

#remove first row
df <- df[-1, ]

#view updated data frame
df

  team points assists rebounds
2    A     99      33       30
3    B     90      28       28
4    C     86      31       24
5    D     88      39       24
6    E     95      34       28

Notice that the first row has been removed.

Also notice that the row names now start at 2.

To reset the row names to start at 1, simply use the following code:

#reset row names
rownames(df) <- NULL

#view updated data frame
df

  team points assists rebounds
1    A     99      33       30
2    B     90      28       28
3    C     86      31       24
4    D     88      39       24
5    E     95      34       28

Example 2: Remove First Row Using dplyr Package

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(team=c(NA, 'A', 'B', 'C', 'D', 'E'),
                 points=c(NA, 99, 90, 86, 88, 95),
                 assists=c(NA, 33, 28, 31, 39, 34),
                 rebounds=c(NA, 30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1 <NA>     NA      NA       NA
2    A     99      33       30
3    B     90      28       28
4    C     86      31       24
5    D     88      39       24
6    E     95      34       28

We can use the slice() function from the dplyr package to remove the first row from the data frame:

library(dplyr)

#remove first row from data frame
df <- df %>% slice(-1)

#view updated data frame
df

  team points assists rebounds
1    A     99      33       30
2    B     90      28       28
3    C     86      31       24
4    D     88      39       24
5    E     95      34       28

Notice that the first row has been removed.

The nice thing about using this approach is that the row numbers are automatically reset after removing the first row.

Additional Resources

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

How to Append Rows to a Data Frame in R
How to Remove Duplicate Rows in R
How to Sum Specific Rows in R

Leave a Reply

Your email address will not be published. Required fields are marked *