从 %>% 管道运算符获取倒数第二个输入

Get second to last input from %>% pipe operator

我想从我刚刚在管道中创建的列中提取一个因子。 我可以使用以下代码执行此操作:

library("dplyr")
library("magrittr")
library("janitor")

iris <- iris %>% janitor::clean_names()    
iris %>% filter(species %in% c("setosa","versicolor")) %>% group_by(species) %>% 
             summarise(mean_sepal_length = mean(sepal_length)) %>% ungroup() %>%
             mutate(species = factor(species, levels = (iris %>% group_by(species) %>%  #<- works but messy
                                                            summarise(mean_sepal_length = mean(sepal_width)) %>% 
                                                            ungroup() %>% arrange(mean_sepal_length) %$% species))) %>% 
                    arrange(species)

我想知道是否有“更清洁”的方法来做到这一点。像这样的东西:

iris %>% filter(species %in% c("setosa","versicolor")) %>% group_by(species) %>% 
     summarise(mean_sepal_length = mean(sepal_length)) %>% ungroup() %>%
     mutate(species = factor(species, levels = (. %>% arrange(mean_sepal_length) %$% species))) %>% 
            arrange(species)

其中 .倒数第二个参数,而不是给管道的最后一个参数?

这会引发错误,因为管道的最后一个参数是 mutate 语句:

Error: Problem with `mutate()` input `species`. x 'match' requires vector arguments i Input `species` is `factor(...)`.

我认为这根本不是管道运算符的工作方式,所以这可能是不可能的。

为了使第二个选项起作用,我们可以将 . 包裹在 {}

library(dplyr)
library(magrittr)
iris %>% 
 filter(species %in% c("setosa","versicolor")) %>%
 group_by(species) %>% 
 summarise(mean_sepal_length = mean(sepal_length)) %>% 
 ungroup() %>%
 mutate(species = factor(species, 
      levels = ({.} %>%
                  arrange(mean_sepal_length) %$% species))) %>%
 arrange(species)
# A tibble: 2 x 2
#  species    mean_sepal_length
#  <fct>                  <dbl>
#1 setosa                  5.01
#2 versicolor              5.94

您可以 arrange 基于 mean_sepal_length 的数据,然后使用 unique.

根据它们的出现分配 factor 级别
library(dplyr)

iris %>% 
  filter(species %in% c("setosa","versicolor")) %>%
  group_by(species) %>% 
  summarise(mean_sepal_length = mean(sepal_length)) %>%
  arrange(mean_sepal_length) %>%
  mutate(species = factor(species, levels = unique(species)))

#  species    mean_sepal_length
#  <fct>                  <dbl>
#1 setosa                  5.01
#2 versicolor              5.94