Select columns based on column value range with dplyr

Viewed 1273

Say I have this dataset:

df <- data.frame(a = rep(1:2, 5), 
                 b = c("value", "character", "string", "anotherstring", "character", NA, "code", "variable", NA, "cell"), 
                 c = c(1, 2, 5, 4, 5, 7, 8, 9, 6, 10),
                 d = rep(2:1, 5), 
                 e = rep(1, 10))

df
   a             b  c d e
1  1         value  1 2 1
2  2     character  2 1 1
3  1        string  5 2 1
4  2 anotherstring  4 1 1
5  1     character  5 2 1
6  2          <NA>  7 1 1
7  1          code  8 2 1
8  2      variable  9 1 1
9  1          <NA>  6 2 1
10 2          cell 10 1 1

I want to select the columns from df whose values are 1 and 2 (so columns a and d only). Assuming that I do not know the column names, is there an efficient way to subset data based on the range of the column's values in dplyr? My initial attempts using select_if and select_at were unsuccessful. Thanks in advance!

1 Answers

You can use :

library(dplyr)
df %>%  select_if(~any(. == 1) & any(. == 2) & all(. %in% 1:2))

#   a d
#1  1 2
#2  2 1
#3  1 2
#4  2 1
#5  1 2
#6  2 1
#7  1 2
#8  2 1
#9  1 2
#10 2 1

which in newer version of dplyr can be written as :

df %>%  select(where(~any(. == 1) & any(. == 2) & all(. %in% 1:2)))

Same in base R Filter :

Filter(function(x) any(x == 1) & any(x == 2) & all(x %in% 1:2) , df)
Related