2

I am using R (version 3.2.3) to recode multiple variables (in the same dataframe) from character values ("High", "Medium", "Low" and "No Concerns") to numeric values (4,3,2 and 1). I know there are several ways to recode a variable and in my example below have been using the "recode" function in the car package. This works fine when recoding a single variable but when I specify multiple variables (columns 45 to 68) all the values are replaced with "N/A".

df[,c(45:68)] <- recode(df[,c(45:68)],"'High'=4;'Medium'=3;'Low'=2;'No Concerns'=1",as.numeric.result=TRUE)

I would appreciate any pointers on where I might be going wrong here. I'm new to the coding community so please let me know if I have provided enough detail in my question.

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 2
    Welcome to StackOverflow! With regard to providing enough detail in your question: Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Mar 31 '16 at 15:02
  • Maybe just use `data.matrix`: e.g. `as.data.frame(Titanic)` vs. `data.matrix(as.data.frame(Titanic))`. – lukeA Mar 31 '16 at 15:06
  • Procrastinatus Maximus - Thanks for the advice regarding asking a good question and providing a workable example. – Matthew Cutmore Mar 31 '16 at 15:12

3 Answers3

3

Try the following:

df[,c(45:68)] <- lapply(df[,c(45:68)], function(x) 
                 recode(x,"'High'=4;
                           'Medium'=3;
                           'Low'=2;
                           'No Concerns'=1",
                            as.numeric.result=TRUE))

What happens here is that you pass individual columns to recode. Looking at the helpfile of recode, you see that the function expects a numeric vector, character vector or a factor as input. In your code you provide a list, however. The above code provides individual columns to recode, which should work. Of course, without proper example data, it is difficult to tell, but give it a try.

coffeinjunky
  • 11,254
  • 39
  • 57
2

A solution with dplyr and hablar:

library(dplyr)
library(hablar)

df <- df %>% 
  mutate_at(vars(45:68),
            funs(case_when(x == 'High'        ~ 4,
                           x == 'Medium'      ~ 3,
                           x == 'Low'         ~ 2;
                           x == 'No Concerns' ~ 1))) %>% 
  convert(num(x))
davsjob
  • 1,882
  • 15
  • 10
0

This worked much better for me, especially since the recode command is more sensitive to formulas:

items<-c("a","b","c")

df[items] <- lapply(df[items], function(x) ifelse(x=="STRONGLY AGREE", 6,
  ifelse(x=="AGREE", 5,
  ifelse(x=="SLIGHTLY AGREE", 4,
  ifelse(x=="SLIGHTLY DISAGREE", 3,
  ifelse(x=="DISAGREE", 2,
  ifelse(x=="STRONGLY DISAGREE", 1,0))))))) 
Ben
  • 1,113
  • 10
  • 26