Reputation: 911
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.
Upvotes: 5
Views: 109
Reputation: 560
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
Upvotes: 0
Reputation: 29153
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
Upvotes: 4
Reputation: 41523
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
Upvotes: 2
Reputation: 1052
library(dplyr)
df %>%
mutate(flag = if_any(everything(), `%in%`, c(1,5))) %>%
filter(!flag)
Upvotes: 4
Reputation: 887731
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
Upvotes: 7