Create R function that can be applied to an entire table