简体   繁体   English

Top_n 返回最大值和最小值 - R

[英]Top_n return both max and min value - R

is it possible for the top_n() command to return both max and min value at the same time? top_n() 命令是否可以同时返回最大值和最小值?

Using the example from the reference page https://dplyr.tidyverse.org/reference/top_n.html使用参考页面https://dplyr.tidyverse.org/reference/top_n.html 中的示例

I tried the following我尝试了以下

df <- data.frame(x = c(10, 4, 1, 6, 3, 1, 1)) 
df %>% top_n(c(1,-1)) ## returns an error

df <- data.frame(x = c(10, 4, 1, 6, 3, 1, 1)) 
df %>% top_n(1) %>%  top_n(-1) ## returns only max value

Thanks谢谢

Not really involving top_n() , but you can try:不是真的涉及top_n() ,但你可以尝试:

df %>%
 arrange(x) %>%
 slice(c(1, n()))

   x
1  1
2 10

Or:或者:

df %>%
 slice(which(x == max(x) | x == min(x))) %>%
 distinct()

Or (provided by @Gregor):或者(由@Gregor 提供):

df %>%
 slice(c(which.min(x), which.max(x)))

Idea similar to @Jakub's answer with purrr::map_dfr想法类似于@Jakub's answer with purrr::map_dfr

library(tidyverse) # dplyr and purrrr for map_dfr

df %>% 
  map_dfr(c(1, -1), top_n, wt = x, x = .)
#    x
# 1 10
# 2  1
# 3  1
# 4  1

Here is an option with top_n where we pass a logical vector based that returns TRUE for min/max using range and then get the distinct rows as there are ties for range ie duplicate elements are present这是一个带有top_n的选项,我们传递一个基于逻辑向量,该向量使用range为最小值/最大值返回 TRUE,然后获取distinct行,因为范围存在关系,即存在重复元素

library(dplyr)
df %>% 
   top_n(x %in% range(x), 1) %>%
   distinct
#   x
#1 10
#2  1

I like @tmfmnk's answer.我喜欢@tmfmnk 的回答。 If you want to use top_n function, you can do this:如果你想使用 top_n 函数,你可以这样做:

df <- data.frame(x = c(10, 4, 1, 6, 3, 1, 1))

bind_rows(
  df %>% top_n(1),
  df %>% top_n(-1)
)

# this solution addresses the specification in comments
df %>%
  group_by(y) %>%
  summarise(min = min(x),
            max = max(x),
            average = mean(x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM