匹配从dplyr summarize_each获得的数据的行

时间:2016-02-07 00:23:21

标签: r dplyr

我想知道Sepal.Length对于每个物种的最小Sepal.Width是什么,但是我无法弄清楚如何获得具有最小Sepal.Width的行的匹配行。

library(dplyr)
itable <- tbl_df(iris)
#print(itable)
a <- itable %>% group_by(Species) %>% summarise_each(funs(min(Sepal.Width)))
print(a)

对不起基本问题。

2 个答案:

答案 0 :(得分:1)

您不需要summarise_each,您需要filter向下到最小的行(如果您愿意,可以select Sepal.Length )。

> iris %>% group_by(Species) %>% filter(Sepal.Width == min(Sepal.Width)) %>% select(Sepal.Length)

Source: local data frame [3 x 2]
Groups: Species [3]

     Species Sepal.Length
      (fctr)        (dbl)
1     setosa          4.5
2 versicolor          5.0
3  virginica          6.0

答案 1 :(得分:1)

我们也可以使用slice

 iris %>%
      group_by(Species) %>% 
      slice(which.min(Sepal.Width))

top_n

iris %>%
     group_by(Species) %>%
     top_n(1, Sepal.Width)