R function code for project2