0

I want know what the Sepal.Length is for the minimum Sepal.Width for each species, but I can't figure out how to get the matching rows for the row that has the minimum Sepal.Width.

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

Sorry for the basic question.

Bell
  • 359
  • 4
  • 9

2 Answers2

1

You don't need to summarise_each, you need to filter down to the rows with the minimums (and maybe select just Sepal.Length, if you like).

> 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
alistaire
  • 42,459
  • 4
  • 77
  • 117
1

We could also use slice

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

Or with top_n

iris %>%
     group_by(Species) %>%
     top_n(1, Sepal.Width) 
akrun
  • 874,273
  • 37
  • 540
  • 662