R code as per teh assignment