Create new dataframe column based on other column in R -


I have looked at similar posts but I have not got anything to work with.

I have a column with 1,2,3,4,5 characters to answer the questions, I need a new column, when the reaction is 1 or 2, no new column Occurs when the reaction is 3, the new column is partially, when the reaction is 4 or 5, the rest is NA.

  data.frame ': 405 of 1 variable: $ SQ023A: chr "-3" "-3" "-3" "-3" ... (this -1, - 2, -3, -4, 1, 2, 3, 4, 5, Yes, no, partially)   

The new column yes, no partially answered but With numbers 4 and 5 with Yes and partially 3.

  test $ sq23 & lt; - ifelse ("4" | "5", sq23 $ SQ23A), "yes", ifelse (("1" | "2", sq23 $ sq23a)  
 , "no", ifelse Ifelse ("3", sq23 $ SQ23A, "partially", "NA"))   
  test $ sq23 & lt; - c ("no"  

, "no", "partial", "yes", "yes") in the form of. Numeric (sq23 $ sq23a)]

Edit:

Your edit, I'm going to give more general solutions to such problems. First we make vectors in which we change the old values ​​we want to change. Then, we define another vector in which the replacement is. Then we move through the match function. For example:

  sq23 your data &create a sample of #create; -data.frame (SQ023A = sample (c (-4: 5, "yes", "no", "partially"), size = 405, replace = TRUE) replace oldValues #define the old values ​​for; -c (1: 5, "yes", "no", "partially") replacement (each value of newValues ​​#define herein of oldValues) newValues ​​& LT; -c ("no", "no", "partial", "yes", "yes", "yes", "no", "partially") for test column sq23 $ test & lt #create ; - New Price [Match (sq23 $ sq023a, old value)]    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -