Home > database >  In R, when I store a variable in an index instead of using it directly, filter doesn't work
In R, when I store a variable in an index instead of using it directly, filter doesn't work

Time:07-18

For example :

filter(data, var=="A") #works

i <- "A"
filter(data, var==i) #empty table

Why ? Thank you.

CodePudding user response:

A possible solution:

library(tidyverse)

df <- data.frame(a = 1:3, b = LETTERS[1:3])

i <- "B"

df %>% 
  filter(b == !!i)
#>   a b
#> 1 2 B
  • Related