R script for min max calculation