remove a row from a dataframe in r

To remove a row from a dataframe in R, you can use the subset() function to create a new dataframe that excludes the row you want to remove. Another way is to use the indexing operator [ ] to remove the row in-place.

Here's an example using the mtcars dataset in R:

main.r
# Load mtcars dataset
data(mtcars)

# Method 1: Using subset() function
mtcars_sub <- subset(mtcars, !(row.names(mtcars) %in% c("Mazda RX4")))
# This creates a new dataframe called mtcars_sub where the row with name "Mazda RX4" is excluded

# Method 2: Using indexing operator [ ]
mtcars_new <- mtcars[-1, ]
# This creates a new dataframe called mtcars_new that excludes the first row in mtcars
395 chars
11 lines

In Method 2, we specified -1 to remove the row with index 1. Similarly, you can specify any row index that you want to remove.

gistlibby LogSnag