Using a variable with the distinct() funciont
0
0
Entering edit mode
2.7 years ago
Mgs3 ▴ 30

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 • 678 views
ADD COMMENT
0
Entering edit mode

You can try this pattern:

uni <- countdata %>%
           distinct(Country)

Hope to help.

ADD REPLY

Login before adding your answer.

Traffic: 1667 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6