Cast benefit analysis improved r function