求 numeric 列的 3 个最低价格的平均值
Find the average of 3 minimum prices of numeric column
我如何找到数字列 (Country_1
) 的 3 个最低价格的平均值?假设我有数千个值?
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))
按值升序对向量进行排序,取前 3 个值,然后计算平均值。
mean(head(sort(d$Country_1), 3))
# [1] 79.72467
如果要对多列执行此操作,请使用 sapply
或 dplyr::across
:
sapply(df[, your_columns], \(x) mean(head(sort(x), 3)))
# or
library(dplyr)
d %>%
mutate(across(your_columns, ~ mean(head(sort(.x), 3)))
如果你只关心最小的3个值,而且数据量很大,使用sort()
和partial = 1:3
效率更高。
mean(sort(sample(d$Country_1), partial = 1:3)[1:3])
选项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
我如何找到数字列 (Country_1
) 的 3 个最低价格的平均值?假设我有数千个值?
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))
按值升序对向量进行排序,取前 3 个值,然后计算平均值。
mean(head(sort(d$Country_1), 3))
# [1] 79.72467
如果要对多列执行此操作,请使用 sapply
或 dplyr::across
:
sapply(df[, your_columns], \(x) mean(head(sort(x), 3)))
# or
library(dplyr)
d %>%
mutate(across(your_columns, ~ mean(head(sort(.x), 3)))
如果你只关心最小的3个值,而且数据量很大,使用sort()
和partial = 1:3
效率更高。
mean(sort(sample(d$Country_1), partial = 1:3)[1:3])
选项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