Solution to R programming assignment 2