Home > Net >  How to delete specific rows conditionally for many variables in a simple way?
How to delete specific rows conditionally for many variables in a simple way?

Time:10-10

Let's say I have this dataframe (but imagine it with hundreds of variables x, y, etc.).

df = data.frame ( x = c(1,2,3,4,5), y = c(1,2,3,4,5))

and I wish to delete the rows that contain either 1 or 5 in any variable.

I am familiar with the following algorithm:

df[!(df$x==1|df$x==5|df$y==1|df$y==5),]

But I am looking for a small function that can handle hundreds of variables at the same time.

CodePudding user response:

Using if_any

library(dplyr)
df %>% 
   filter(!if_any(everything(), ~ .x %in% c(1, 5)))

-output

  x y
1 2 2
2 3 3
3 4 4

CodePudding user response:

library(dplyr)
df %>%
  mutate(flag = if_any(everything(), `%in%`, c(1,5))) %>%
  filter(!flag)

CodePudding user response:

Just another base solution:

df[!apply(df, 1, function(x) any(x %in% c(1, 5))),]

#>   x y
#> 2 2 2
#> 3 3 3
#> 4 4 4

CodePudding user response:

You could use the following code:

df = data.frame ( x = c(1,2,3,4,5), y = c(1,2,3,4,5))
df[rowSums(df==1|df==5)==0,]
#>   x y
#> 2 2 2
#> 3 3 3
#> 4 4 4

Created on 2022-10-07 with reprex v2.0.2


df = data.frame ( x = c(1,2,3,4,5), y = c(1,2,3,4,5))
df[rowSums(df[-1]==1|df[-1]==5)==0,]
#>   x y
#> 2 2 2
#> 3 3 3
#> 4 4 4

Created on 2022-10-07 with reprex v2.0.2

CodePudding user response:

In base R:

df[apply(df, 1, \(x){!any(x %in% c(1,5))}),]
#>   x y
#> 2 2 3
#> 3 3 3
#> 4 4 4

In a function:

delete_rows <- function(d, n){
  d[apply(d, 1, \(x){!any(x %in% n)}),]
}
delete_rows(df, c(1,3,4))
#>   x y
#> 5 5 5
  • Related