Libraries

library('data.table')


Data

df_raw <- fread('https://raw.githubusercontent.com/Brent-Morrison/Misc_scripts/master/stock_data.csv')
df_raw$date_stamp <- as.Date(df_raw$date_stamp, "%d/%m/%Y")


Return top n by group

# Select columns and date required
df <- df_raw[date_stamp == as.Date('2021-06-30'), 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 <- data.table(df, key = 'sector')
df <- df[ , head(.SD, 5), by = sector]


Print

# Print table
rmarkdown::paged_table(df)