Reputation: 2085
I have a dataset with variables group
and X1:X1000
. For each out of columns X1:X1000
I want to filter the rows with 0
value only and then return the unique values of group
variable in filtered dataset. In case of single column it would be something like:
df %>% filter(X1 == 0) %>% select(group) %>% unique()
Since I want this for each column I thought that maybe map
can be used somehow to return the list of unique values of group
with respect to columns X1:X1000
, but can't figure out what the call should look like.
Upvotes: 1
Views: 1925
Reputation: 887551
We can use map
to loop through the string of column names, with filter_at
, specify the string name, to filter the rows, select
the 'group' and get the distinct
elements
library(tidyverse)
map(paste0("X", 1:1000), ~
df %>%
filter_at(vars(.x), all_vars(.==0)) %>%
select(group) %>%
distinct
)
Or instead of filter_at
, the string can be converted to symbol (sym
) and evaluate (!!
) within filter
map(paste0("X", 1:1000), ~
df %>%
filter(!! (rlang::sym(.x)) ==0) %>%
select(group) %>%
distinct
)
Upvotes: 2