0 votes
in R Language by
Distinct number of observation in R language

1 Answer

0 votes
by

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)) % > %
	arrange(desc(number_player))
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

Related questions

0 votes
asked Nov 14, 2019 in R Language by MBarbieri
0 votes
asked Nov 14, 2019 in R Language by MBarbieri
...