A function in R to easily apply functions accross indexes to a dataset