Question:
Consider the following data.frame
and the variable x
df <- data.frame(x = c(rep(0, 10), rep(1, 10)), y = 1:20)
x <- 0
Tried using dplyr
to select elements from column x
equals global variable x
library(dplyr)
df %>% filter(x == x))
But I got the entire data.frame
in the response. The filter must be considering only the column, I imagine.
How do I tell the filter
that one of the x
's is a global variable?
Answer:
Use .GlobalEnv$x
:
library(dplyr)
df %>%
filter(x == .GlobalEnv$x)
x y
1 0 1
2 0 2
3 0 3
4 0 4
5 0 5
6 0 6
7 0 7
8 0 8
9 0 9
10 0 10