База R Решение:
price_summary <-
data.frame(do.call("rbind", lapply(split(
df, paste(df$Date, df$price_1, sep = " - ")
),
function(x) {
data.frame(
Date = unique(x$Date),
price_1 = unique(x$price_1),
price_2 = mean(x$price_2[which.min(abs(x$price_2 - x$price_1))]),
Volat = mean(x$Volat),
stringsAsFactors = FALSE
)
})),
row.names = NULL)
Данные:
df <- structure(
list(
Date = structure(c(
15170, 15170, 15170, 15170,
15170, 15171, 15171
), class = "Date"),
price_2 = c(215L, 217L,
235L, 240L, 200L, 203L, 207L),
price_1 = c(200, 200, 200, 200,
201.5, 205, 205),
Volat = c(5, 6, 5.5, 5.3, 6.2, 6.4, 5.1)
),
row.names = c(NA,-7L),
class = "data.frame"
)