I am trying to recode some psychometric scales for scoring in R. Often the scales will come in the form of a factor that will need to be converted to a number to calculate the score; for example ("Never" = 0, "Sometimes" = 1, "Always" = 2).
I am having limited success in scoring specific numbers. If the scale starts from 1 (e.g. "Never" = 1, "Sometimes" = 2, "Always" = 3) then everything seems to work okay, however if the scale starts from 0 (or some other number other than 1), the conversion to numeric doesn't go as expected. I have found a temporary solution, but it is rather cumbersome as I need to convert to a factor, then character and finally to numeric.
What I am trying to do is have R assign a number to each specific level of the factor and then return the number when converting to numeric. For example if I want "Never" = 0, "Sometimes" = 1 and "Always" = 2 then R would return:
> answers <- c("Never", "Sometimes", "Always", "Always", "Sometimes", "Never")
> some_function(answers)
[1] 0 1 2 2 1 0
My temporary and less-than-ideal solution is do do the following:
> as.numeric(as.character(fct_recode(as_factor(answers),
+ "0" = "Never",
+ "1" = "Sometimes",
+ "2" = "Always")))
[1] 0 1 2 2 1 0
If I try to run the above code without converting to character then it doesn't return what I am after:
> as.numeric(fct_recode(as_factor(answers),
+ "0" = "Never",
+ "1" = "Sometimes",
+ "2" = "Always"))
[1] 1 2 3 3 2 1
Does anyone know how I can more efficiently convert a factor variable numeric and assign specific numeric values to the levels of the factors?
Thanks!