Jen
Jen

Reputation: 205

Create new dataframe column based on other column in R

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

Upvotes: 0

Views: 457

Answers (2)

fileunderwater
fileunderwater

Reputation: 1125

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

Upvotes: 2

nicola
nicola

Reputation: 24480

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

Upvotes: 2

Related Questions