Home > Enterprise >  How to subset a dataset ito remove rows with certain values in any column?
How to subset a dataset ito remove rows with certain values in any column?

Time:01-08

So I have a dataframe like the below and I want to remove all rows with columns a, b or c containing values equal to one of a few numbers.

i.e. in the below if I wanted to remove the rows with entries equal to 1, I would want only the second row (I want to ignore the first few columns).

nothing a b c
1 1 2 8
1 2 3 2
1 4 6 1

Returns :

nothing a b c
1 2 3 2

Should note there are many columns after the first few that I want to ignore, so I don't want to name them individually.

CodePudding user response:

A solution using base-r.

## identify which rows in the df contain 1s
rows_to_remove = which(df[,-1] == 1, arr.ind=T)[,1]
# subset these rows
df[-rows_to_remove,]
  nothing a b c
2       1 2 3 2

CodePudding user response:

You can use dplyr::if_any.

df %>% 
  filter(!if_any(c(a,b,c), ~ .x == 1))
  nothing a b c
1       1 2 3 2

You can also select with - or : if the columns are consecutive:

filter(df, !if_any(-nothing, ~ .x == 1))
filter(df, !if_any(a:c, ~ .x == 1))

Data

df <- structure(list(nothing = c(1L, 1L, 1L), a = c(1L, 2L, 4L), b = c(2L, 
3L, 6L), c = c(8L, 2L, 1L)), class = "data.frame", row.names = c(NA, -3L))

CodePudding user response:

In this case we could use filter with across and an anonymous function without if_any.

library(dplyr)

filter(df, across(a:c, ~. != 1))
  nothing a b c
1       1 2 3 2

CodePudding user response:

Try this

df[rowSums(df[-1]==1)==0,]
  •  Tags:  
  • Related