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.

3
  • 2
    Welcome to StackOverflow! With regard to providing enough detail in your question: Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you. Commented Mar 31, 2016 at 15:02
  • Maybe just use data.matrix: e.g. as.data.frame(Titanic) vs. data.matrix(as.data.frame(Titanic)). Commented Mar 31, 2016 at 15:06
  • Procrastinatus Maximus - Thanks for the advice regarding asking a good question and providing a workable example. Commented Mar 31, 2016 at 15:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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))

Comments

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))))))) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.