0

I have looked at similar posts but haven't gotten anything to work.

I have a column with characters 1,2,3,4,5 which are answers to interview questions I want a new column that when the response is 1 or 2 the new column is a No, when the response is 3 the new column is Partly, when the response is 4 or 5 is Yes, all else is NA.

data.frame':    405 obs. of  1 variables:
$ SQ023A        : chr  "-3" "-3" "-3" "-3" ...(this has -1, -2, -3, -4, 1, 2, 3, 4, 5, Yes, No, Partly)

The new column should keep the Yes, No, Partly answers but replace the 1 and 2 with No, 4 and 5 with Yes, and 3 with Partly. All else is NA.

I have tried the following without success

sq23$test <- ifelse(("1"|"2", sq23$SQ23A), "No",
             ifelse("4"|"5", sq23$SQ23A), "Yes",
             ifelse("3", sq23$SQ23A), "Partly","NA"))
1

2 Answers 2

2

Try:

   sq23$test <- c("No","No","Partly","Yes","Yes")[as.numeric(sq23$SQ23A)]

Edit:

In light of your edit, I'm going to give a more general solution for this kind of problems. First we build a vector containing the old values we want to replace. Then, we define another vector in which there is the replacement. Then we do the trick through the match function. For instance:

    #create a sample of your data 
    sq23<-data.frame(SQ023A=sample(c(-4:5,"Yes","No","Partly"),size=405,replace=TRUE))
    #define the old values to replace
    oldValues<-c(1:5,"Yes","No","Partly")
    #define the replacement (each value of newValues replace the corresponding of oldValues)
    newValues<-c("No","No","Partly","Yes","Yes","Yes","No","Partly")
    #create the test column
    sq23$test<-newValues[match(sq23$SQ023A,oldValues)]
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm I get an error with no column added > sq23$test <- c("No","No","Partly","Yes","Yes")[as.numeric(sq23$SQ023A)] Error in c("No", "No", "Partly", "Yes", "Yes")[as.numeric(sq23$SQ023A)] : only 0's may be mixed with negative subscripts In addition: Warning message: NAs introduced by coercion
I saw your edit and changed my answer. Have a look and see if it helps.
THANKS :-) That was a really neat trick. I exported the file and checked it for accuracy. I also checked the method below which works if you don't have existing Yes, No, or Partly in the original column. If you do it over writes them with NA and the interviewee's response is lost. Thanks again, you made my Friday. sq23$test<- NA sq23$test[sq23$SQ23A == 1 | sq23$SQ23A == 2]<- "No" sq23$test[sq23$SQ23A == 4 | sq23$SQ23A == 5]<- "Yes" sq23$test[sq23$SQ23A == 3]<- "Partly"
2

For clarity, I would do:

sq23$test<- NA
sq23$test[sq23$SQ23A == 1 | sq23$SQ23A == 2]<- "No"
sq23$test[sq23$SQ23A == 4 | sq23$SQ23A == 5]<- "Yes"
sq23$test[sq23$SQ23A == 3]<- "Partly"

Based on your edit, and for a more general case, you can also use a dictionary type solution:

values<- c("no", "no", "partly","yes","yes","yes","no","partly")  # new value
names(values)<- c(1:5, "yes", "no", "partly")   # keys
> values
       1        2        3        4        5      yes       no   partly 
     "no"     "no" "partly"    "yes"    "yes"    "yes"     "no" "partly" 
sq23$test<- values[as.character(sq23$SQ23A)]  
# as.character() used to make sure that the keys/old values are passed as 
# characters, and not e.g. a factor

2 Comments

Thank you very much for your help. Your code answered my initial question and will work in some other situations I am dealing with. Have a good day.
@Jen I've added another more general solution based on your edits, just to show another way it can be done (beside nicola's).

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.