Hi, everyone. I'm trying to create a custom function for some basic counts and percentages of answers to some Likert scale survey questions to make the code shorter, since I have to use it with a lot of questions. It worked at first, but when I tried to add a mutate function into the custom function, to change a variable into a factor variable and arrange the values in an order other than alphabetical order, R gave me an error message (before I could try and use the custom function on any data). Here's the code:
library(tidyverse)
library(janitor)
satisfaction <- function(df, varname) {
df %>%
drop_na({{varname}}) %>%
filter({{varname}} != "Not applicable") %>%
tabyl({{varname}}) %>%
adorn_pct_formatting(digits = 0) %>%
mutate({{varname}} = factor({{varname}},
levels = c("Very satisfied",
"Satisfied",
"Neither satisfied nor dissatisfied",
"Dissatisfied",
"Very dissatisfied"))) %>%
arrange({{varname}})
}
Here's the error message I'm getting:
Error: unexpected '=' in:
" adorn_pct_formatting(digits = 0) %>%
mutate({{varname}} ="
If I take the "mutate" and "arrange" lines out of the code, everything works, but the rows in the output objects end up out of order. There must be a way to include "mutate" in a custom function. Am I missing something here?
Thanks for any help anyone can give me.