Updated R script for coursera programming assignment 2