I am looking for some way to delete specific rows by row numbers using dplyr's pipe function
library(dplyr)
head(mtcars)
Now let say I want remove row numbers c(1, 4, 7). Typically we would use mtcars[-c(1, 4, 7), ] to do the same.
However I want to use pipe to do the same.
Is there any way to do this?
Any pointer will be highly appreciated.
We can use slice
library(dplyr)
mtcars %>%
slice(-c(1, 4, 7))
Or if we want to use [
mtcars %>%
`[`(-c(1, 4, 7),)
Or use extract from magrittr
library(magrittr)
mtcars %>%
extract(-c(1, 4, 7),)
We could use filter
library(dplyr)
# vector for rows to remove
to_remove <- c(1, 4, 7)
mtcars %>%
filter(!row_number() %in% to_remove)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With