top of page
  • Writer's picture♫Shokolatte♬

WTF Facts - recode (R)

Updated: Mar 6, 2022

As a programming language learner, I try to decode the meaning of the functions all the time and keep the memo. Sometimes different functions do exactly the same job but deep down, they usually have different functional methods even if they seem to spit out the same outcome. I use the function `identical()` to check if those functions are the same, and 80% of times, it returns `FALSE`.

Today, I was totally not expecting this but since it turned out to be a pretty surprising consequence, I'll just leave it here. It's the `recode()` by {dplyr} and `fct_recode()` by {forcats} that are proved identical, even though the arguments are slightly different.

Making a vector of factors;

wtf <- factor(c("apple", "bum", "banana", "republic"))

then rename the factors using `fct_recode()` and `recode()`;

fct_recode(wtf, fruit = "apple", fruit = "banana")


[1] fruit    bum      fruit    republic
Levels: fruit bum republic


recode(wtf, "apple" = "fruit", "banana" = "fruit")


[1] fruit    bum      fruit    republic
Levels: fruit bum republic

They do seem the same, right? Now check for exact equality using `identical()`;

identical(fct_recode(wtf, fruit = "apple", fruit = "banana"), recode(wtf, "apple" = "fruit", "banana" = "fruit"))


[1] TRUE


I am pretty amazed - how about you?😅 Maybe I just have a weird sense but I just thought it was worth recording so I'm using this space. A matter of fact, I'm making this a series as I find, so stick around for more trivial findings!

Recent Posts

See All


bottom of page