1

I have a string with the same name as a variable in a tibble. If I want to filter using this string, I have the following working options:

jahr <- 2020

test <- tibble(jahr = c(2019, 2020), fte2020 = c(10, 11), fte2021 = c(12,13))

spalte <- paste0("fte", jahr)

# Using get:
test %>% 
  filter(base::get(spalte) == 11)
# Using all in one:
test %>% 
  filter(base::get(paste0("fte", .env$jahr)) == 11)
# Using .data:
test %>% 
  filter(.data[[spalte]] == 11)

I thought that something like

test %>% 
  filter( {{ spalte  }} == 11)

would also work. But it doesn't. Is this not possible or am I missing something?

Cheers Renger

arnyeinstein
  • 669
  • 1
  • 5
  • 14

1 Answers1

1

You can use the sym a tidy eval tools for details look here https://rlang.r-lib.org/reference/sym.html and here https://stackoverflow.com/a/48219802/13046806

test%>%
  filter((!!sym(spalte))==11)
#> # A tibble: 1 × 3
#>    jahr fte2020 fte2021
#>   <dbl>   <dbl>   <dbl>
#> 1  2020      11      13

Created on 2023-04-14 with reprex v2.0.2

Wael
  • 1,640
  • 1
  • 9
  • 20