babylinguist babylinguist - 2 days ago 6
R Question

Deleting columns from a data frame based on presence/absence of certain values

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
, thus the next step should be piped after the
filter
command. I have tried using
summarise
in conjunction with apply, but that has not worked.

Answer

You can use select_if():

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

#  x
#1 1
Comments