Home > Software design >  Remove cases with a specific value in R
Remove cases with a specific value in R

Time:11-09

I want to remove all rows with value 9999 in my dataset. Here is an example dataset:

df <- data.frame(a=c(1, 3, 4, 6, 9999, 9),
                 b=c(7, 8, 8, 7, 13, 16),
                 c=c(11, 13, 9999, 18, 19, 22),
                 d=c(12, 16, 18, 22, 29, 38))

So the final dataset will only contain row 1, 2, 4 and 6. I have a large dataset and so how to do this without specifying the names of all columns? Thank you!

CodePudding user response:

You could do:

df[which(apply(df, 1, \(i) !any(i == 9999))),]
#>   a  b  c  d
#> 1 1  7 11 12
#> 2 3  8 13 16
#> 4 6  7 18 22
#> 6 9 16 22 38

CodePudding user response:

 df[-which(df == 9999, TRUE)[,1], ]
  a  b  c  d
1 1  7 11 12
2 3  8 13 16
4 6  7 18 22
6 9 16 22 38

CodePudding user response:

An option with dplyr

library(dplyr)
df %>% 
   filter(!if_any(everything(), ~ . == 9999))

-output

  a  b  c  d
1 1  7 11 12
2 3  8 13 16
3 6  7 18 22
4 9 16 22 38

Or with across

df %>%
    filter(across(everything(), ~ . != 9999))
  • Related