This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Using a variable with the distinct() funciont

Greetings, I have a dataset as such:

countdata<-fread("table.txt",data.table=F)
countdata
 A1     B1  Country
11465   7   AT
12323   6   AT
21321   6   BT
21311   7   CT
12321   8   CT

I need to extract unique values of each column, output desired for the Country column

Country
AT
BT
CT

I managed to do that with the distinct() function of dplyr. My issue is when I try to input a variable, for instance

> condition = "Country"
> uni <- distinct(countdata, condition)
Error in `distinct_prepare()`:
! `distinct()` must use existing variables.
✖ `Condition` not found in `.data`.

Following other posts i tried with !! or {{ }} unsuccesfully:

> condition = "Country"
> uni <- distinct(countdata, !!condition)
> uni
    "Country"
   1   Country
> 
> uni <- distinct(countdata, {{condition}})
> uni
"Country"
1   Country
quoteless <- noquote(condition)
> uni <- distinct(countdata, !!quoteless)
> uni
 <noquote>
1   Country

How can i solve this issue?

dplyr r

You can try this pattern:

uni <- countdata %>%
           distinct(Country)

Hope to help.

0 answers

No answers yet.

Log in to answer this question.