HappyCoding
HappyCoding

Reputation: 5139

R why n_distinct provides different results?

I assumed the following two usages should provides the same results. apparently not. anyone can help to explain a bit?

fr = data.frame(A=c(1,2,3,1),B=c(T,F,T,F) )

> fr  %>% n_distinct(.$A)
[1] 4

> n_distinct(fr$A)
[1] 3

Upvotes: 1

Views: 282

Answers (2)

yeedle
yeedle

Reputation: 5008

fr %>% n_distinct(.$A) is equivalent to n_distinct(fr, fr$A) which will evaluate the entire data frame, not just the A column.

Try fr %>% n_distinct() and then try fr$A %>% n_distinct() to see the difference for yourself.

Upvotes: 2

akrun
akrun

Reputation: 886938

We need to extract the 'A' column and then apply the n_distinct

fr %>%
   .$A %>%
    n_distinct

Upvotes: 0

Related Questions