completed R functions for Assignment2