1. Learn
  2. /
  3. Courses
  4. /
  5. Developing R Packages

Exercise

R function for converting distances

The unitConverter package you've been building can be even more versatile with a function converting distances from feet to meters and vice versa. This function could be invaluable for international collaborators, who frequently need to convert between these units.

Here you'll create a function accepting a numeric distance value, the unit of this input value, and the unit to which you want to convert. Then you'll store this function in the package directory. You'll use a conditional structure within your function for the four possible conversions: feet to meters, meters to feet, feet to feet (no change), and meters to meters (no change).

To convert feet to meters, multiply the feet value by 3.2808. To convert meters to feet, multiply the meter's value by 0.3048.

Instructions 1/2

undefined XP
    1
    2
  • Define an R function dist_converter with three arguments without defaults: dist_value, unit_from, and unit_to.
  • Convert the input distance from the specified unit to the desired unit via a conditional flow.
  • Use your function to convert 100 meters into feet.