I have following dataset:
> head(dela)
card date BWHA BWHA_annotation
1 MSD-1555 20210710 4.8791685 0
2 MSD-1555 20210710 2.4018840 0
3 MSD-1555 20210710 0.7337054 0
4 MSD-1555 20210710 -0.4382136 0
5 MSD-1555 20210710 -1.8340851 0
6 MSD-1555 20210710 -1.9579161 0
which contains 15 different cards (column 'card'), each card contains 30 day sampling period and each day contains **10 recordings (in this case column date) **. My target is to subset top 3 values of BWHA scores (column BWHA ) per day. SO I need top 3 filtered based on two groups: sites and days.
I tried, typical way of extracting top N values, but it is only applicable for one group.
d <- data.table(dela, key="card")
d.out <- d[, .SD[BWHA %in% tail(sort(unique(BWHA)), 3)], by=card]
Any help would be appreciated,