简化 R 中的 dplyr 代码以选择数据集中的最小值

Simplify dplyr code in R for selecting minimum value in a dataset

我需要打印产品数据集中 Premium 产品最低价格的前 3 个月:

   Date          Grade   Price
   <chr>         <ord>    <dbl>
 1 January 2016  Regular   22.20
 2 January 2016  Premium   32.45
 3 January 2016  Premium   55.34
 4 February 2016 Regular   23.69
 ... with 100 more rows

结果将如下所示:

      Date          Price
      <chr>         <dbl>
1 March 2017         21.13
2 May 2017           21.35
3 October 2017       21.67

使用dplyr,我有如下代码,但是好像有点长。有什么办法可以简化这个吗?

min <- select(product, Date, Price)
min <- filter(min, Grade == "Premium")
min <- arrange(min, Price)
min[1:3,]

如果我们想避免重复赋值,使用链式(%>%)。看起来这些步骤是独特的步骤,在dplyr

中可能无法简化
 library(dplyr)
 product %>%
      select(Date, Price)  %>%
      filter(Grade == 'Premium') %>%
      arrange(Price) %>%
      slice_head(3)

base R中,我们可以简化这个

out <- subset(product, select = c(Date, Price), subset = Grade == 'Premium')
head(out[order(out$Price),], 3)