Functions

This section takes an estimated 45 minutes to complete.

Functions play a critical role in any R program and you tend to write a lot of them while programming. This section focuses on functions in R.

Functions in R

To start, watch the following videos outlining the concept of functions in R:

R exercises

  1. Create a function which will take the height and weight values and calculate body mass index.

  2. Write an R function that will generate a Fibonacci sequence of desired size.

  3. Write an R function which will perform transcription of dna letters into RNA.

  4. Write a function is.even(n) in R that returns TRUE when n is even and FALSE otherwise. Then, using is.even(n), write a function evens.in(v) that returns a vector composed of the even integers in a vector v of integers.

  5. Create a function that given a vector and an integer will return how many times the integer appears inside the vector.

  6. Create a function that given an integer will calculate how many divisors it has (other than 1 and itself). Make the divisors appear by screen.

swirl Exercises

Complete swirl Lesson 9: Functions, to solidify what you just learned, and return here afterward.

Congratulations!

If you made it here, then congratulations! You have successfully completed this section. Move to the next portion of the guide with the arrow buttons below.

Last updated