I have a variable with the same name as a column in a dataframe:
df <- data.frame(a=c(1,2,3), b=c(4,5,6))
b <- 5
I want to get the ro
Recently I have found this to be an elegant solution to this problem, although I'm just starting to wrap my head around how it works.
df %>% filter(b == !!b)
which is syntactic sugar for
df %>% filter(b == UQ(b))
A high-level sense of this is that the UQ (un-quote) operation causes its contents to be evaluated before the filter operation, so that it's not evaluated within the data.frame.
This is described in this chapter of Advanced R, on 'quasi-quotation'. This chapter also includes a few solutions to similar problems related to non-standard evaluation (NSE).