How to Remove a Row From a Data Frame in R

preview_player
Показать описание
Removing rows is a common data cleaning task. This video shows four different ways to do it.

Code used in this clip:

# Remove a single row:
df[-3,]

# Remove multiple rows at once:
df[-c(3,4,5),]

# Remove row by row index name
df[(rownames(df) != 'Datsun 710'),]

# Remove rows by a column value
df[!(df$cyl == 6),]

Code Clips are basic code explanations in 2 minutes or less. They are intended to be short reference guides that provide quick breakdowns and copy/paste access to code needed to accomplish common data science tasks. Think Stack Overflow with a video explanation.

* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! For R that means I may use = for assignment and the special Unicode large < and > symbols in place of the standard sized ones for dplyr pipes and comparisons. These special symbols should work as expected for R code on Windows, but may need to be replaced with standard greater than and less than symbols for other operating systems.
Рекомендации по теме
Комментарии
Автор

i wish that every lesson was as easy as this tutorial

alexmarvin
Автор

i cannot delete it, the result: Error in df[-c(12, 13), ] : object of type 'closure' is not subsettable

heartheart
Автор

Does not work, only returns an error message

collinmeredith
Автор

On the last method: We removed rows with 6 cyl, what if I want to keep rows with 6 cyl? What function should I write?

mshahriarsonet
Автор

This doesn't remove a row from the data frame itself. It creates only a view.

tayyabhasan