Common R function to format dataframe