changed R code according to assignment