I posted a better example here: Calling user defined function to calculate column values - General - RStudio Community