And maybe you'll find the example provided in this topic How to Vectorize a function in R RStudio helpful as well.