首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择每个组值的顶部x%-保留行ID

选择每个组值的顶部x%-保留行ID
EN

Stack Overflow用户
提问于 2019-09-17 14:03:24
回答 1查看 49关注 0票数 0

我试图找出每个分水岭前15%的分数,但在打印结果时保留多边形ID。

代码语言:javascript
复制
# here's a small example dataset (called "data"):

 polygon watershed score
        1         1    61
        2         1    81
        3         1    16
        4         2    18
        5         2    12
        6         3    78
        7         3    81
        8         3    20
        9         3    97
       10         3    95

# I obtain the top 15% using this method:

top15 <- (data %>% select(watershed, score) %>%
  group_by(watershed) %>%
  arrange(watershed, desc(score)) %>%
  filter(score > quantile(score, 0.15)))


# results look like this:

      <int> <int>
1         1    81
2         1    61
3         2    18
4         3    97
5         3    95
6         3    81
7         3    78

在打印结果时,如何包含列“多边形”?

非常感谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-17 14:11:04

在您的语句中,您只选择了watershedscore,但排除了polygon。因此,删除select语句,您应该得到您想要的东西。此外,安排没有增加价值,所以我删除了它:

代码语言:javascript
复制
library(dplyr)
mdat <- structure(list(polygon = 1:10, 
                       watershed = c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), 
                       score = c(61L, 81L, 16L, 18L, 12L, 78L, 81L, 20L, 97L, 95L)), 
                       class = "data.frame", row.names = c(NA, -10L))


mdat %>% 
  group_by(watershed) %>%
  filter(score > quantile(score, 0.15))
# # A tibble: 7 x 3
# # Groups:   watershed [3]
#   polygon watershed score
#     <int>     <int> <int>
# 1       1         1    61
# 2       2         1    81
# 3       4         2    18
# 4       6         3    78
# 5       7         3    81
# 6       9         3    97
# 7      10         3    95
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57976000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档