Data

df_raw <- read.csv('https://raw.githubusercontent.com/Brent-Morrison/Misc_scripts/master/stock_data.csv')


Return top n by group

# Select columns and date required
df <- df_raw[df_raw['date_stamp'] == '30/06/2021', c('symbol','date_stamp','sector','asset_growth','roa','roe','leverage','ttm_earnings')]

# Order 
df <- df[order(df$ttm_earnings, decreasing = TRUE), ]

# Filter for top 5 
df <- Reduce(f = rbind, x = by(df, df['sector'], head, n = 5))


Print

# Print table
rmarkdown::paged_table(df)