Function foo below recodes the numeric column var1 into a string column (1 --> "a", 2 --> "b" etc.).
I was wondering how to vectorize this function such that instead of argument var1 we have argument ...?
That is, all variables provided in ... should undergo what var1 undergoes in foo.
A possible useful post might be HERE.
library(tidyverse)
foo <- function(data, var1, caps = FALSE, reverse = FALSE){
let <- if(caps) base::LETTERS else base::letters
FUN <- if(reverse) utils::tail else utils::head
data %>% mutate(var1 = factor(FUN(let, max(var1))[var1]))
}
# EXAMPLE OF USE:
(dat <- data.frame(var1 = c(2,1,3,1,4:1), id = 1:8))
foo(dat, var1, caps = T, reverse = T)