R functions completed for Assignment 2