是否知道如何将字符串排序变量传递给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.
有什么建议吗?
答案 0 :(得分:0)
啊解决了
mpg %>% mutate(manufacturer = fct_reorder(manufacturer, .[[order_var]]))