dimanche 9 juillet 2017

In r studio, "If... else" function for individual levels in another column

So I've already posted this question but I think the title didn't really reflect what I was trying to ask.

I've seen a few questions like my own - asking to apply a function dependent on subsets or another condition.

However, I have tried two suggestions and neither have worked.

Here is what I've got so far:

Question Key    Response      Response 2    True Answer
STAI_Calm      Moderately so    3   
STAI_Secure    Somewhat         2   
STAI_Tense     Very much so     4   
STAI_Regretful Not at all       1   

I want to apply a function to SOME of my data in Response 2

So if Question Key = STAI_Calm or STAI_Secure NOT STAI_Tense or STAI_Regretful then a function will be applied to Response 2 (5 - Response 2)

I don't mind if this changes the values in Response 2 or is in a new column (True Answer)

I've tried:

    True_Answers <- if(Question_Key == "STAI_Calm" | "STAI_Secure"|{5 -
 Response2}

Which threw up:

Error: unexpected '=' in "True_Answers <- if(Question_Key ="

and I've tried:

setDT(STAI)[Question_Key == "STAI_Calm" | "STAI_Secure",  5 - Response2]

Which caused error:

Error in Question_Key == "STAI_Calm" | "STAI_Secure" 
  operations are possible only for numeric, logical or complex types

If anyone could help, that would be great!

Aucun commentaire:

Enregistrer un commentaire