R dplyr: Non-Standard Evaluation difficulty. Would like to use dynamic variable names in filter and mutate

后端 未结 2 1149
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 03:31

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

2条回答
  •  盖世英雄少女心
    2021-01-14 04:23

    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))
    

提交回复
热议问题