banner



how to delete a row in r

Delete Rows from R Data Frame

In this tutorial, we will learn how to delete a row or multiple rows from a data frame in R programming with examples.

You cannot actually delete a row, but you can access a data frame without some rows specified by negative index. This process is also called subsetting in R language.

To delete a row, provide the row number as index to the Data frame. The syntax is shown below:

mydataframe[-c(row_index_1, row_index_2),]

where

  • mydataframe is the data frame
  • row_index_1, row_index_2, . . . are the comma separated indices which should be removed in the resulting data frame

A Big Note: You should provide a comma after the negative index vector -c(). If you miss that comma, you will end up deleting columns of the data frame instead of rows.

Example 1 – Delete Row from Data Frame

Let us create a data frame, DF1

> DF1 = data.frame(V1= c(1, 5, 14, 23, 54), V2= c(9, 15, 85, 3, 42), V3= c(9, 7, 42, 87, 16)) > DF1   V1 V2 V3 1  1  9  9 2  5 15  7 3 14 85 42 4 23  3 87 5 54 42 16 >

Let us assume that we need DF1 with 2nd row deleted. The index of 2nd row is ofcourse 2. Now, we will access this data frame with a negative index and store the result in another data frame DF2.

> DF2 = DF1[-c(2),] > DF2   V1 V2 V3 1  1  9  9 3 14 85 42 4 23  3 87 5 54 42 16 >

Viola. We have created a new data frame with a row deleted from the previous data frame.

Example 2 – Delete Multiple Rows from Data Frame

Let us create a data frame, DF1

> DF1 = data.frame(V1= c(1, 5, 14, 23, 54), V2= c(9, 15, 85, 3, 42), V3= c(9, 7, 42, 87, 16)) > DF1   V1 V2 V3 1  1  9  9 2  5 15  7 3 14 85 42 4 23  3 87 5 54 42 16 >

Let us assume that we need DF1 with 2nd and 4th rows deleted. The indices are (2,4). Now, we will access this data frame with a vector of negative indices and store the result in another data frame DF2.

> DF2 = DF1[-c(2, 4),] > DF2   V1 V2 V3 1  1  9  9 3 14 85 42 5 54 42 16 >

Viola. We have created a new data frame with multiple rows deleted from the previous data frame.

Conclusion

In this R Tutorial, we have learnt how to delete a row or multiple rows from a data frame in R.

how to delete a row in r

Source: https://www.tutorialkart.com/r-tutorial/r-dataframe-delete-row/

Posted by: rosshunned.blogspot.com

0 Response to "how to delete a row in r"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel