Table of Contents
To remove the first row from a data frame in R, you can use the `[-1,]` indexing method. This will remove the first row and return the remaining rows as a new data frame. Alternatively, you can use the `slice()` function from the `dplyr` package to select all rows except the first one. Both of these methods will effectively remove the first row from the data frame.
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 28We 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 28Example 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 28We can use the 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:
Cite this article
stats writer (2024). How can I remove the first row from a data frame in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-the-first-row-from-a-data-frame-in-r/
stats writer. "How can I remove the first row from a data frame in R?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-the-first-row-from-a-data-frame-in-r/.
stats writer. "How can I remove the first row from a data frame in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-the-first-row-from-a-data-frame-in-r/.
stats writer (2024) 'How can I remove the first row from a data frame in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-the-first-row-from-a-data-frame-in-r/.
[1] stats writer, "How can I remove the first row from a data frame in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I remove the first row from a data frame in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
