1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Writing Functions in R

Connected

Exercise

Calling functions

One way to make your code more readable is to be careful about the order you pass arguments when you call functions, and whether you pass the arguments by position or by name.

gold_medals, a numeric vector of the number of gold medals won by each country in the 2016 Summer Olympics, is provided.

For convenience, the arguments of median() and rank() are displayed using args(). Setting rank()'s na.last argument to "keep" means "keep the rank of NA values as NA".

Best practice for calling functions is to include them in the order shown by args(), and to only name rare arguments.

Instructions 1/2

undefined XP
  • 1

    The final line calculates the median number of gold medals each country won.

    Rewrite the call to median(), following best practices.

  • 2

    The final line calculates each country's ranking by number of gold medals. It uses negative gold_medals so that the country with the most medals will have 1st place: the largest positive value in gold_medals is the smallest ("most negative") value in -gold_medals.

    Rewrite the call to rank(), following best practices.