dplyr
dplyr copied to clipboard
A subtle scoping flaw in pull()
If x
is not a column name in data frame d
, then pull(d, x)
attempts to look up the value of x
in the environment instead of returning NULL or an error as I would expect.
library(dplyr)
# Create two env-variables
d <- data.frame(x = "ex", y = "why")
x <- "y"
# As expected, pulls the data-variable `x`
pull(d, x)
# If name `x` does not exist in `d`,
d <- select(d, -x)
# Not expected: finds the env-variable `x`, pulls the data-variable `y`
pull(d, x)
# Exactly as if we had written pull(d, y)
identical(pull(d, x), pull(d, y))