babylinguist - 3 months ago 23

R Question

I would like to subset a data frame by removing columns that meet or do not meet a certain condition. For example, given the following data:

`df <- data.frame(w = c('a', 'b', 'c'),`

x = c(1, 0, 0),

y = c(0, 1, 0),

z = c(0, 0, 1))

Which gives:

`w x y z`

a 1 0 0

b 0 1 0

c 0 0 1

I would like to remove columns that contain a 0 after subsetting the rows. For example:

`df %>% filter(., w == 'a')`

Produces:

`w x y z`

a 1 0 0

Which I would like to then reduce to:

`x`

1

I am looking to do this using

`dplyr`

`filter`

`summarise`

Answer

You can use `select_if()`

:

```
df %>% filter(w == 'a') %>% select_if(function(col) is.numeric(col) && all(col != 0))
# x
#1 1
```