0

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?

2
  • distinct will return 1st value of duplicate. See distinct(my.df, uid, .keep_all= TRUE) which is x for uid = 1. Do you want to return the last value of duplicate i.e y instead ? Commented Sep 24, 2020 at 2:12
  • I don't mind, I can use either duplicate. Commented Sep 24, 2020 at 2:20

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

The tidyselect across/all_of idiom works as well:

distinct(my.df, across(all_of(iKey)), .keep_all= TRUE)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.