Can you please clarify for me when to use the count() versus the n() function?
This will help me to understand why the following codes gave two different outputs.
Programming in R
Code 1
fueleconomy::vehicles %>%
distinct(model, make ) %>%
group_by( model ) %>%
count() %>%
filter( n > 1 ) %>%
arrange( desc( n ))
Code 1 output
A tibble 60 X 2
Groups: model [60]
Code 2
fueleconomy::vehicles %>%
distinct(model, make ) %>%
group_by( model ) %>%
filter( n() > 1 ) %>%
arrange( model )
Code 2 output
A tibble 126 X 2
Groups: model [60]
Note: I was expecting the two codes to give the same output but they didn't. So, I'm confused and would like some clarifications of the main difference between the n() and the count() functions.
Also, when can one use either in favour of the other?
Can both be used together in certain circumstances?
P.s: I'm a beginner with no programming background and self-learning, so, be gentle.
Thank you in advance for your help.