I'm pretty brand new to R, so please excuse any R grammer errors here...
I have a 19 question likert survey saved in a data frame (19 columns). The responses are numeric and converted to characters.
I would like to change the Likert scores to factor and specify levels. I can do this with each column individually, but wondering how to use a loop to apply this to all columns without having to repeat this line of code 19 times for all columns
My current code
lik_tab$lik_01 <- factor(lik_tab$lik_01,
levels = c("1", "2", "3", "4", "5"),
ordered = TRUE)
I know I can repeat that for the next columns "lik_02", lik_03"... and so on, but how do I write this more succinctly?
I tried lapply with the following error:
lik_tab <- lapply(lik_tab,factor(levels = c("1", "2", "3", "4", "5"),
+ ordered = TRUE))
Error in match.fun(FUN) :
'factor(levels = c("1", "2", "3", "4", "5"), ordered = TRUE)' is not a function, character or symbol
Thanks!
If we wrap with the function call, use anonymous function, also while assigning to the full dataset, use
[]to keep the structure of the dataset, otherwise, it would be alistoutput from thelapplyOtherwise, we can just specify the parameters