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

后端 未结 2 1122
隐瞒了意图╮
隐瞒了意图╮ 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:11

    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
    
    0 讨论(0)
  • 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))
    
    0 讨论(0)
提交回复
热议问题