completed functions for R Programming assignment 2