How can I find the average of the 3 minimum prices of a numeric column (Country_1
) ?Imagine that I have thousands of values?
d<-structure(list(Subarea = c("SA_1", "SA_2", "SA_3", "SA_4", "SA_5",
"SA_6", "SA_7", "SA_8", "SA_10", "SA_9"), Country_1 = c(101.37519256645,
105.268942332558, 100.49933368058, 104.531597221684, NA, 83.4404308144341,
86.2833044714836, 81.808967345926, 79.6786979951661, 77.6863475527052
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-10L))
CodePudding user response:
Sort your vector by ascending value, take the 3 first values, and compute the mean.
mean(head(sort(d$Country_1), 3))
# [1] 79.72467
Use sapply
or dplyr::across
if you want to do that to multiple columns:
sapply(df[, your_columns], \(x) mean(head(sort(x), 3)))
# or
library(dplyr)
d %>%
mutate(across(your_columns, ~ mean(head(sort(.x), 3)))
CodePudding user response:
If you only care the minimal 3 values and the amount of data is large, using sort()
with partial = 1:3
is more efficient.
mean(sort(sample(d$Country_1), partial = 1:3)[1:3])
CodePudding user response:
An option with slice_min
library(dplyr)
d %>%
slice_min(n = 3, order_by = Country_1) %>%
summarise(Mean = mean(Country_1))
# A tibble: 1 × 1
Mean
<dbl>
1 79.7