简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM