简体   繁体   中英

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

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.

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
library(dplyr)
df %>%
  mutate(flag = if_any(everything(), `%in%`, c(1,5))) %>%
  filter(!flag)

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM