First version of the R funtions for assignment 2