commented r script to explain mathematical operations