Nov 14, 2019 in R Language
Q: Distinct number of observation in R language

1 Answer

0 votes
Nov 14, 2019

The function n() returns the number of observations in a current group. A closed function to n() is n_distinct(), which count the number of unique values.

In the next example, you add up the total of players a team recruited during the all periods.

# distinct values
data % > %
	group_by(teamID) % > %
	summarise(number_player = n_distinct(playerID)) % > %
Code Explanation

  • group_by(teamID): Group by year and team
  • summarise(number_player = n_distinct(playerID)): Count the distinct number of players by team
  • arrange(desc(number_player)): Sort the data by the number of player
Click here to read more about Loan/Mortgage
Click here to read more about Insurance