将字符串变量传递给forcats :: fct_reorder

时间:2019-01-31 10:04:35

标签: r tidyverse forcats

是否知道如何将字符串排序变量传递给fct_reorder

require(dplyr)
require(forcats)
require(ggplot2)

order_var = 'displ'

mpg %>% mutate(manufacturer = fct_reorder(manufacturer, order_var))
#> Error in mutate_impl(.data, dots): Evaluation error: length(f) == length(.x) is not TRUE.

尝试过bang bang !!

mpg %>% mutate(manufacturer = fct_reorder(manufacturer, !!order_var))
#> Error in mutate_impl(.data, dots): Evaluation error: length(f) == length(.x) is not TRUE.

尝试过eval

as.name(eval(order_var))
#> displ

mpg %>% mutate(manufacturer = fct_reorder(manufacturer, as.name(eval(order_var))))
#> Error in mutate_impl(.data, dots): Evaluation error: length(f) == length(.x) is not TRUE.

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

啊解决了

mpg %>% mutate(manufacturer = fct_reorder(manufacturer, .[[order_var]]))