Complete R Code of Assignment 2

Implement required functionality.