y = 1
library(dplyr)
data.frame(x = 1, y = 2) %>%
mutate(xy = x+y)
In the above example, xy will equal 3, but I want to use the global variable y to compute xy. How can I do that with dplyr?
We can use (!!)
library(dplyr)
data.frame(x = 1, y = 2) %>%
mutate(xy = x+ !!y)
-output
# x y xy
#1 1 2 2
Or extract directly from the .GlobalEnv
data.frame(x = 1, y = 2) %>%
mutate(xy = x+ .GlobalEnv$y)
-output
# x y xy
#1 1 2 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With