找到数字列的最低价格的平均值
如何找到数字列的3个最低价格的平均值(country_1
)?想象一下我有数千个值?
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))
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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过上升值,取3个第一个值并计算平均值来对矢量进行排序。
使用
sapply
或dplyr ::跨
如果要对多列这样做:Sort your vector by ascending value, take the 3 first values, and compute the mean.
Use
sapply
ordplyr::across
if you want to do that to multiple columns:如果您仅关心最小3个值,并且数据量较大,则使用
sort()
使用partial = 1:3
更有效。If you only care the minimal 3 values and the amount of data is large, using
sort()
withpartial = 1:3
is more efficient.slice_min
的选项An option with
slice_min