functions to R programming Assignment2