Coursera R programming course: assignment 3