首页 > 解决方案 > 为什么 stringr 和 forcats 中替换的命名约定不同?

问题描述

我很难记住将键值对传递给stringr::str_replace_all()and的正确命名约定forcats::fct_recode()。显然,这两个函数在是否需要命名字符串向量或单个命名字符串以及对的顺序方面有所不同:

library("tidyverse")

ggplot2::diamonds %>%
  mutate(cut = stringr::str_replace_all(cut, 
                               c("Ideal" = "Awesome","
                                 Premium" = "Deluxe")))

ggplot2::diamonds %>%
  mutate(cut = forcats::fct_recode(cut, 
                          "Awesome" = "Ideal", 
                          "Deluxe" = "Premium"))

知道这两个函数做不同的事情,命名约定不同是有原因的吗?也许约定只是任意的——如果不是,我想了解底层逻辑。

标签: rstringrforcats

解决方案


推荐阅读