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!