Automated calculations using the apply family of functions