How to Remove the First Row in R From a Data Frame

Sometimes when doing data processing on a data frame you may need to remove the first row. Within R programming there is a simple procedure for this process. This procedure can be used for any row in the data frame, but we are just removing the first one.

Description

This procedure has the format of data-frame[- 1, ], where “data-frame” is replaced by the variable name of the data frame that you are working with. Now the “-1” could be substituted for any negative number, but if you use another negative number you would remove that row number and not the first one. As you can tell it is a really simple process to use and consequently one that you are likely to not have any problems with. All you need to do is place the [-1] right after the data frame variable name.

Explanation

When you use this procedure, it removes the row number being referred to by the negative number in the brackets. In this specific case that we are dealing with, that number is a negative one, as a result, the first row is removed. If you use a negative two, the second row gets removed. A negative three removes the third row and so on. If you place a positive number between the brackets, the procedure will keep only that row number. This makes it a handy procedure for accessing individual rows.

Examples

Here are two examples of this procedure in action.

> df = data.frame(A = c(5, 6, 7, 8),
+ B = c(1, 2, 3, 4),
+ C = c(“D”, “E”, “F”, “G”),
+ D = c(“A”, “B”, “C”, “D”))
> df
A B C D
1 5 1 D A
2 6 2 E B
3 7 3 F C
4 8 4 G D
> df2 = df[- 1, ]
> df2
A B C D
2 6 2 E B
3 7 3 F C
4 8 4 G D

In this example, we have a basic data frame consisting of letters and numbers. When we apply this procedure with the negative on, you can see that it removes the first row.

> df = data.frame(A = as.integer(abs(rnorm(4)*10)),
+ B = as.integer(abs(rnorm(4)*10)),
+ C = as.integer(abs(rnorm(4)*10)),
+ D = as.integer(abs(rnorm(4)*10)))
> df
A B C D
1 6 14 3 10
2 4 0 23 7
3 5 8 11 9
4 5 0 0 11
> df2 = df[- 1, ]
> df2
A B C D
2 4 0 23 7
3 5 8 11 9
4 5 0 0 11

In this example, we have a basic data frame consisting of randomly generated integers. When we apply this procedure with the negative one, you can see that it removes the first row. Combined these examples show this procedure in different cases.

Application

This procedure has three main applications. The first is the removal of a specific row and particularly in this case the first row. You will want to remove the first row if it contains test or formatting data. The second application would be retrieving a specific row for the purpose of placing it in its own variable. The third application of this procedure is simply reducing the size of a large data frame.

This is a simple little application that not only removes the first row of a data frame easily but any row that you want. It also allows you to extract a row for separate use. It is a helpful tool that gives you better control over the content of a data frame.

Scroll to top
Privacy Policy