(see "IMPORTANT" note below - question is not solved by the answers to a similar post; it's not a duplicate)
I have a bunch of dataframes with a ton of vectors containing the same two likert scales that I need to recode from strings (currently as factors) to numeric values (1 to 5). Here are the scales:
Likert scale A:
---------------
Terrible = 1
Below Average = 2
Average = 3
Above Average = 4
Excellent = 5
Likert Scale B:
---------------
Strongly disagree = 1
Somewhat disagree = 2
Meh = 3
Somewhat agree = 4
Strongly agree = 5
As an example, here is HAVE1
, one of the many datasets (each of which have a different number of vectors and different vector names) with these likert patterns:
Apples Oranges Bananas ServiceGood ShortTime
Excellent Terrible Average Somewhat agree Somewhat agree
Excellent Above Average Strongly agree Somewhat agree
Above Average Terrible Below Average Somewhat disagree
Excellent Average Below Average Meh Strongly disagree
Below Average Terrible Above Average Somewhat agree Meh
… … … … …
I need to replace the character strings with a numeric equivalent from the likert scale codes listed above. For example, the first 5 observations of WANT1
should look like this:
Apples Oranges Bananas ServiceGood ShortTime
5 1 3 4 4
5 NA 4 5 4
4 1 2 NA 2
5 3 2 3 5
2 1 4 4 3
… … … … …
I'm looking for efficient ways to modify these values that don't require any invocation of vector names due to the differences in names and positions across all my HAVE
dataframes. Any thoughts?
IMPORTANT: this problem is NOT solved by any of the examples provided in the question "Dictionary style replace multiple items"; every answer throws an error of some sort or fills the values of most variables with NA/replaces the wrong values.
For example, the following code...
map = setNames(c("Excellent", "Above Average", "Below Average", "Average", "Terrible"),
c("5", "4", "2", "3", "1"))
HAVE1[] <- map[unlist(HAVE1)]
Sets every value in my file to NA except for the empty cells, which it codes to "Terrible". The only solution from the other question that works in the "dictionaries" answer is the solution proposed in the question itself by the asker - it is this very approach that the asker hoped to avoid.