I am trying to understand a problem regarding the pull()
function using these two examples:
df <- data.frame(x = c("ex", "ex"), y = c("why", "why"))
d <- df
x <- "y"
pull(d, x)
d <- select(df, -x)
pull(d, x)
pull(d, y)
# - - - - - -
Why does this output look like the pull()
function is working well but the output before was not working?
library(dplyr)
# Using the dataframe “mtcars”
df <- mtcars
# Define a variable
x <- 'mpg'
# Using pull() to extract column "mpg"
resultado <- pull(df, x)
print(resultado)
# Remove column "mpg"
df <- select(df, -x)
# Using pull() again with variable x, which still contains "mpg"
resultado <- pull(df, x)
print(resultado)
In this second case thepull(df, x)
should be different right? Not an error.
In both chunks of code I should have the same result which is a vector. But at the second code using mtcars
as datframe I get this error:
Error in
pull(): ! Can't extract columns that don't exist. ✖ Column
mpg doesn't exist.
Considering this article: https://graphdr.github.io/data-stories/posts/2024-08-01-subtle-flaw-pull/
5