Created
October 20, 2015 04:04
-
-
Save mbjones/7236e46671074c31dc23 to your computer and use it in GitHub Desktop.
demonstrate use of dplyr::filter with lapply, both with and without NSE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example R code to filter data using criteria passed in via lists, using lapply | |
# Matt Jones 2015-10-19 | |
library(dplyr) | |
# create a sample fake data frame | |
df <- cbind(expand.grid(sciName=list("A", "B", "C", "D"), family=list("X", "Y"), stage=list("S1", "S2", "S3", "S4")), count=1) | |
df | |
# set up our filter conditions | |
condition1 <- list(sciName="A", stageVector=c("S2", "S3")) | |
condition2 <- list(sciName="C", stageVector=c("S3", "S4")) | |
conditionList <- list(condition1, condition2) | |
# Define the filtering function | |
filterStages <- function(condition, df) { | |
subset.data <- df %>% | |
filter(sciName == condition$sciName) %>% | |
filter(stage %in% condition$stageVector) | |
return(subset.data) | |
} | |
# demo the filter function working on a single condition at a time | |
filterStages(condition1, df) | |
filterStages(condition2, df) | |
# demo the filter function working over a list of conditions | |
resultDataList <- lapply(conditionList, filterStages, df) | |
resultDataList | |
## TRY IT ALL DIFFERENTLY WITH SE rather than NSE so that we can choose filter variables dynamically | |
library(lazyeval) | |
condition1 <- list(taxonField="sciName", taxonValue="A", stageVector=c("S2", "S3")) | |
condition2 <- list(taxonField="family", taxonValue="Y", stageVector=c("S3", "S4")) | |
conditionList <- list(condition1, condition2) | |
filterStages <- function(condition, df) { | |
cond <- interp(~ tf == tv, tf = lazy(condition$taxonField), tv = condition$taxonValue) | |
subset.data <- df %>% | |
filter_(interp(~ tf == condition$taxonValue, tf = as.name(condition$taxonField))) %>% | |
filter(stage %in% condition$stageVector) | |
return(subset.data) | |
} | |
filterStages(condition1, df) | |
filterStages(condition2, df) | |
resultDataList <- lapply(conditionList, filterStages, df) | |
resultDataList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment