I have created a reproducible example to illustrate the problem I am having with non-standard evaluation in R (dplyr). I would like to use dynamic variable names in the scen
Here is one option with parse_expr
from rlang
library(rlang)
library(dplyr)
patientData %>%
filter(!! parse_expr(paste(firstDateName, ">", secondDateName)))
# patientID birth_d treat_d death_d
#1 5 2017-01-01 2011-12-27 2012-12-26
#2 7 2011-06-25 2012-06-24 2001-01-01
#3 12 2018-05-05 2013-09-17 2014-09-17
1) rlang Use sym
like this:
library(dplyr)
library(rlang)
firstDateName <- sym("birth_d")
secondDateName <- sym("death_d")
badRecords <- patientData %>% filter(!!firstDateName > !!secondDateName)
giving:
> badRecords
patientID birth_d treat_d death_d
1 5 2017-01-01 2011-12-27 2012-12-26
2 7 2011-06-25 2012-06-24 2001-01-01
3 12 2018-05-05 2013-09-17 2014-09-17
2) Base R or in base R:
firstDateName <- "birth_d"
secondDateName <- "death_d"
is.bad <- patientData[[firstDateName]] > patientData[[secondDateName]]
badRecords <- patientData[is.bad, ]
2a) subset Another base solution would be to replace the last two lines above with:
subset(patientData, get(firstDateName) > get(secondDateName))