Home > front end >  How to explicitly specify that a name refers to a variable, rather than a column name, in dplyr?
How to explicitly specify that a name refers to a variable, rather than a column name, in dplyr?

Time:08-20

I have the following chunk of code:

gap <- 1000  

HCE <- HCE %>%
   dplyr::mutate(ordered = gap * (as.numeric(outcome) - 1)   original)

I want to dismbiguate, in the mutate directive, that gap refers to the variable, not to a "gap" column. How can I do so?

CodePudding user response:

The .data and .env pronouns make it explicit where to find objects when programming with data-masked functions.

HCE %>%
  mutate(ordered = .env$gap * (as.numeric(outcome) - 1)   original)

Or use the injection operator !!:

HCE %>%
  mutate(ordered = !!gap * (as.numeric(outcome) - 1)   original)
Reference
  1. Injection operator !!
  2. Injecting env-variables with !!
  • Related