如何解决从%>%管道运算符获取倒数第二个输入
我想从我刚在管道中创建的列中考虑因素。 我可以使用以下代码进行此操作:
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
的数据,然后使用factor
根据unique
的级别分配它们。
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。