The R code has been updated for the assignment.