updated R file with solution