J.Doe
J.Doe

Reputation: 549

Easy way to delete rows that have a certain value

I have a data set with several columns and I'm working on it using R. Most of those columns have missing data, which was set as a value -200. What I want to do is to delete all the rows that have -200 in any of the columns. Is there an easy way to do this other than going by each column at a time? Can I delete all rows that a value of -200 all at once?

Thank you for your time!

Upvotes: 1

Views: 68

Answers (2)

akrun
akrun

Reputation: 886978

A tidyverse option would be

library(tidyverse)
df %>%
   filter_all(all_vars(. != -200))

data

df <- data.frame(v1 = c(-200, 1, 2, 3), v2 = c(1, -200, 2, 4))

Upvotes: 2

Sotos
Sotos

Reputation: 51582

You can use rowSums(), i.e.

df[rowSums(df == -200) == 0,]

Upvotes: 2

Related Questions