Integrating ideas from Stack Overflow to improve our R code (CC287)