It would be possible to abbreviate the following script:
- not to use so many chained operations.
- Avoid using .SD and by as much as possible
library(data.table)
DT<-structure(list(title = c("a", "a", "a", "a", "b", "b", "b", "b", "c", "c", "c", "c", "d", "d", "d", "d"), date = c("12-07-2020", "13-07-2020", "14-07-2020", "15-07-2020", "12-07-2020", "13-07-2020",
"14-07-2020", "15-07-2020", "12-07-2020", "13-07-2020", "14-07-2020", "15-07-2020",
"12-07-2020", "13-07-2020", "14-07-2020", "15-07-2020"),
bucket = c(1, 1, 1, 4, 9, 7, 10, 10, 8, 5, 5, 5, 8, 10, 9, 10),
score = c(86, 22, 24, 54, 66, 76, 43, 97, 9, 53, 45, 40, 21, 99, 91, 90)),
row.names = c(NA, -16L), class = c("data.table","data.frame"))
DT[DT[, .I[bucket == min(bucket)], by = title]$V1]
DT[, .SD[which(bucket == min(bucket))], by =title][,
`:=`(avg_score = mean(score)), by = .(title)][,
.SD[.N,c(1,2,4)], by = .(title)]
the original code is a scrip using dplyr .:RStudio Community
tt <- data %>%
group_by(title) %>%
filter(bucket == min(bucket)) %>%
mutate(avg_score = mean(score)) %>%
slice_max(date) %>%
select(-score)
>
title date bucket avg_score
<chr> <chr> <dbl> <dbl>
1 a 14-07-2020 1 44
2 b 13-07-2020 7 76
3 c 15-07-2020 5 46
4 d 12-07-2020 8 21
>