Search code examples
rdataframemeanconditional-formatting

Find the average of 3 minimum prices of numeric column


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))

Solution

  • 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)))