R code for a function to calculate multidiversity