R - Select elements of a data frame with a column that has the same name as a global variable with 'dplyr'

4

Consider the following data.frame and variable x

df <- data.frame(x = c(rep(0, 10), rep(1, 10)), y = 1:20)
x <- 0

I tried to use dplyr to select elements from column x equal to global variable x

library(dplyr)
df %>% filter(x == x))

But I got all the data.frame in the response. The filter should be considering only the column, I imagine.

How do I indicate for filter that one of the x is a global variable?

    
asked by anonymous 10.04.2018 / 22:53

1 answer

4

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
    
10.04.2018 / 23:34