r - How to filter a column by multiple, flexible criteria -


i'm writing function aggregate dataframe, , needs applicable wide variety of datasets. 1 step in function dplyr's filter function, used select data ad campaign types relevant task @ hand. since need function flexible, want ad_campaign_types input, makes filtering kind of hairy, so:

aggregate_data <- function(ad_campaign_types) {   raw_data %>%     filter(ad_campaign_type == ad_campaign_types) -> agg_data   agg_data } new_data <- aggregate_data(ad_campaign_types = c("campaign_a", "campaign_b", "campaign_c")) 

i think above work, while runs, oddly enough returns small fraction of filtered dataset should be. there better way this?

another tiny example of replaceable code:

ad_types <- c("a", "a", "a", "b", "b", "c", "c", "c", "d", "d") revenue <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) data <- as.data.frame(cbind(ad_types, revenue))  # now, filtering select ad types "a", "b", , "d", # should leave 7 values new_data <- filter(data, ad_types == c("a", "b", "d")) nrow(new_data) [1] 3 

for multiple criteria use %in% function:

filter(data, ad_types %in% c("a", "b", "d")) 

you can use "not in" criterion:

filter(data, !(ad_types %in% c("a", "b", "d"))) 

however notice %in%'s behavior little bit different ==:

> c(2, na) == 2 [1] true   na > c(2, na) %in% 2 [1]  true false 

some find 1 of more intuitive other, have remember difference.

as using multiple different criteria use chains of criteria and/or statements:

filter(mtcars, cyl > 2 & wt < 2.5 & gear == 4) 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -