I'm looking to set a string variable at the start of a script that I then use to filter a data set with. For the example below, I want to use distinct() to pick rows with distinct cyl. When I run distinct(cyl, .keep_all =T) in the snippet below, I get the right result. But if I want to change that variable easily (like switching the distinct field to mpg), the code below fails. Any advice on how to successfully evaluate the key_distinct_field field in the code below so that it pulls the distinct cyl rows?
The problem is that dplyr (and other tidyverse packages) tend to expect arguments to be variable / column names. So distinct is looking for a column in mtcars named "key_distinct_field" (and of course not finding it).
The following works: mtcars_tibble |> distinct(get(key_distinct_field), .keep_all =T)
@prubin Just realized the get() function used in your solution creates a new column titled get(key_distinct_field) in the date frame. I can de-select the new column, but ideally would want to avoid having that new column generated all together
It's not entirely clear what the mission here is. The following produces a tibble with a single column named "key" containing the distinct value of the chosen field: mtcars_tibble |> distinct("key" = get(key_distinct_field), .keep_all = F)
resulting in