Reputation: 138
Suppose I have a dataframe:
> my.df <- data.frame(uid=c(1,1,3),somevalue=c("x","y","z"))
> my.df
uid somevalue
1 1 x
2 1 y
3 3 z
I want:
uid somevalue
1 1 y
2 3 z
I can't use
distinct(my.df, uid, .keep_all= TRUE)
because the variable name "uid"
could change. However I have the variable name stored as a string.
So I get
> iKey <- "uid"
> distinct(my.df, iKey, .keep_all= TRUE)
uid somevalue
1 1 x
2 1 y
3 3 z
Warning message:
Trying to compute distinct() for variables not found in the data:
- `iKey`
This is an error, but only a warning is raised for compatibility reasons.
The operation will return the input unchanged.
How can I get distinct() to use the value of iKey
and not take it literally?
Upvotes: 0
Views: 749
Reputation: 21284
The tidyselect across
/all_of
idiom works as well:
distinct(my.df, across(all_of(iKey)), .keep_all= TRUE)
Upvotes: 1
Reputation: 389325
We can use .data
pronoun :
library(dplyr)
distinct(my.df, .data[[iKey]], .keep_all= TRUE)
# uid somevalue
#1 1 x
#2 3 z
Or convert iKey
to symbol with sym
and evaluate it !!
distinct(my.df, !!sym(iKey), .keep_all= TRUE)
Upvotes: 1