Convert for-loops to lapply results in significant runtime improvements for larger datasets.