Reputation: 433
How do I use the dplyr::filter()
function with column numbers, rather than column names?
As an example, I'd like to pick externally selected columns and return the rows that are all zeros. For example, for a data frame like this
> test
# A tibble: 10 x 4
C001 C007 C008 C020
<dbl> <dbl> <dbl> <dbl>
1 -1 -1 0 0
2 0 0 0 0
3 1 1 0 0
4 -1 -1 0 0
5 0 0 0 -1
6 0 0 0 1
7 0 1 1 0
8 0 0 -1 -1
9 1 1 0 0
10 0 0 0 0
and a vector S = c(1,3,4)
How would I pick all the rows in test
where all(x==0)
? I can do this with an test[apply(test[,S] 1, function(x){all(x==0)},]
but I'd like to use this as part of a %>%
pipeline.
I have not been able to figure out the filter()
syntax to use column numbers rather than names. The real data has many more columns (>100) and rows and the column numbers are supplied by an external algorithm.
Upvotes: 5
Views: 2969
Reputation: 13135
Use filter_at
with all_vars
library(dplyr)
df %>% filter_at(c(1,3,4), all_vars(.==0))
C001 C007 C008 C020
1 0 0 0 0
2 0 0 0 0
Upvotes: 4